How to create a custom API route in SocialEngine Zend - zend-framework

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?

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.

Related

how to change default module in Zend 2

I am new to ZendFeamerwork version 2. I could easily change the default controller in Zend1 but it seems very difficult to me to find out how to change default module in Zend2.
I searched over google but there is no easy solution.
I just created a module named "CsnUser" I can access this module via the following url
http://localhost/zcrud/public/csn-user/
I want csn-user to load instead of "application" module i.e url should be
http://localhost/zcrud/public/
or
http://localhost/zcrud/
Please let me know how to get this done.
Based on #Hoolis comment:
You have to set that action on this route
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'That\Namespace\CsnUser',
'action' => 'index'
)
)
)
)
In the skeleton application this route is set in the Application Module, but you can move this somewhere or edit it.

Zend 2 router setting

Im very mew to ZF2, and cant figure out how to set the global router.
I know how to set on module level:
http://packages.zendframework.com/docs/latest/manual/en/user-guide/routing-and-controllers.html says:
The mapping of a URL to a particular action is done using routes that
are defined in the module’s module.config.php file. We will add a route
for our album actions. This is the updated config file with the new
code commented.
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
Is there a way to config a default behaviour for the whole application? Or i have to confing in each module?
config/application.config.php would be a logical place for it. Is it somewhere documented?
What your ZF2 module defines in getConfig() gets merged with all the other module configs and eventually replaced by modules loaded afer it if some config keys collide. Thus your getConfig() already affects the entire application and its scope is not limited to your module only.

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );

Zend Framework routing

I'm trying to setup some routes for my ZF app but not getting too far. I have a controller 'WebServiceController', it has an index action and a lookupTransaction action. I want to use routes like this:
ws/
ws/lookupTransaction
Ideally I'd like anything with a 'ws/' prefix to go to the WebServiceController and match the action name. I'm not sure how to do that yet but I am trying to get each route working so I added these two routes:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws/lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
}
}
The first one works as expected but the second one doesn't, I just get 'Application Error'. What am I doing wrong? Just out of interest, if I remove my two routes and try and go to:
web-service/lookup-transaction
I still get the same error!
Solved
Here is how I can make it work with camel cased action name and camel cased URL.
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws-lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
Thanks
Ziad
Try just this one route as a solution to both problems:
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
the action parameter then serves as a default, so if no action is specified in the URL, index will be used. Otherwise it will route to the action in the URL. So example.com/ws/lookupTransaction will go to lookuptransactionAction() in your controller.
If this still gives you an error, post the error message so we can see what the problem is.
The router actually transforms URLs to lowercase. So the correct URL should be all lowercase dash separated words. Also I'm not sure if it's possible to use slash in route name (the first parameter of addRoute()).

Zend Framework Router Getting /module/VALUE/controller/action

I've been googling around and I can't seem to find anything which explains the use of ZF router well. I've read the documentation on the site, which seems to only talk about re-routing.
I am trying to make the format:
/module/value/controller/action give /module/controller/action passing on value as a parameter
e.g.
/store/johnsmithbigsale/home/newstuff would route to /store/home/newstuff passing on johnsmithbigsale as the value to a parameter with a hidden namespace e.g. storeName.
Some help would be greatful!
You can use Zend_Controller_Router_Route to map your url parts to modules, controllers, actions, and parameters that can be used in the controller by $this->_getParam('varName'). You can define these routes in the application.ini file or in the application bootstrap.
// custom city route
$route = new Zend_Controller_Router_Route(
'cities/:city',
array(
'controller' => 'city',
'action' => 'view'
)
);
$this->addRoute('city', $route);
// custom buy widgets route
$route = new Zend_Controller_Router_Route_Regex(
'buy_(.+)_widgets/([0-9]+)(.*)',
array(
'controller' => 'widgets',
'action' => 'view'
),
array(
1 => 'nothing',
2 => 'widget_id',
3 => 'vars'
)
);
$this->addRoute('widgets', $route);
The regex route is kind of specific to my app, but you can see that each match can get mapped to a parameter.