zend-frameworksocialengine

How to create a custom API route in SocialEngine Zend


I have created a new REST API module in SocialEngine which can be browsed via http://server_address/mymodule or http://server_address/mymodule/index. I have a controller class Mymodule_IndexController inside thecontrollers directory. It has a method indexAction in which I output some JSON response. It works.
The question is, how can I add another route and corresponding action e.g. food/browse in this module. I have already added the following routes inside manifest.php, but when I browse to http://server_address/mymodule/browse, the route is not resolved (Page not found error).

'routes' => array(       
    'food_general' => array(
        'route' => 'advancedrestapi/:controller/:action/*',
        'defaults' => array(
            'module' => 'advancedrestapi',
            'controller' => 'index',
            'action' => 'index',
        ),
        'reqs' => array(
            'controller' => '\D+',
            'action' => '\D+',
        ),
    ),

How can I introduce new custom routes and corresponding PHP method to my module?


Solution

  • To add a custom route, you need to add a file with the same name as your 'action' and then .tpl extension. So, for the route in question ('action'=>'browse'), you will need to have a file as application/modules/mymodule/views/scripts/index/browse.tpl. The file can be empty.
    Then, you will need to add a new method to your IndexController class browseAction (action + Action). Write your logic inside the method and you will be able to access the action via http://server_address/mymodule/index/browse.