SocialEngine Tutorial Adding custom routes for your modules using Bootstrap.php

When you are developing your SocialEngine module or widgets you might want to be able to pass parameters to an action within your controller. For example;

http://yourwebsite.com/yourmodule/your-controller/your-action/your-variable

Now to put it into context, say we are building a profile viewer module (called profile) and we had a controller (user) that shows a user’s profile (using the displayUserDetailsAction) based on the ID you you provide to it. The current URL would look like this;

http://yourwebsite.com/profile/user/displayUserDetails/1 (unique user_id)

As you can see this is a rather long url for the user to type, a more clean solution would be to have a URL which looks like this;

http://yourwebsite.com/profile/1

In order to achieve this you can use the Bootstrap.php file located within the root of your module which handles all the custom routing for actions within your module. You will want to create the method “protected function _initRouter()”, fetch an instance of the router and then add the custom route to it.

class YOURMODULE_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
    // You will need to add this method
    protected function _initRouter(){
        $fc = Zend_Controller_Front::getInstance();
        $router = $fc->getRouter();
        $router->addRoute('UNIQUE_ROUTE_NAME', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
        return $router;
    }
 
}

Now what this will do is route any request of http://yourwebsite.com/profile/anynumber to the module of profile using the user controller and call the displayUserDetails action passing in the variable of user_id. In order to get this id you have to use the code below within your action;

$user_id = $this->getRequest()->getParam("user_id");

Now as I found out you might want to have multiple routes within your module. If you want to do this then you have to have unique route names within your bootstrap.php file!! Took me a while to realise why they weren’t working but you just have to add;

class YOURMODULE_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
    // You will need to add this method
    protected function _initRouter(){
        $fc = Zend_Controller_Front::getInstance();
        $router = $fc->getRouter();
        $router->addRoute('UNIQUE_ROUTE_NAME1', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
$router->addRoute('UNIQUE_ROUTE_NAME2', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
        return $router;
    }
 
}

Take note of unique_route_name1 and 2!!

Happy coding

Credit to this guy who had an initial post on routing
http://tjgamble.com/2011/04/adding-custom-routes-to-your-socialengine-4-modules/