Multiple Routes with Standard Router (Zend Framework) - zend-framework

I am trying to set up multiple routes to the same controller in zend as such:
URL | Controller::Action
=================================================================================
http://mysite/tasks/:level/ | Objectives::Objectives
http://mysite/tasks/:level/:objective/ | Objectives::tasks
http://mysite/tasks/:level/:objective/:taskID/ | Objectives::view
I've tried the following:
<?php
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/:taskID/',
array(
'controller' => 'objectives',
'action' => 'view'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
?>
However the last rule seems to overwrite the previous rules in the router.. I've read the Zend Documentation for the router over and over, I have a feeling im just missing something - should I be using a different router class?
Any help is Much Appreciated

As suspected I was overwriting the previous rules. The first argument for addRoute() is a name for the route, not the controller you are routing to as I thought. Giving each route a unique name fixed the problem.

Related

Zend Framework: Router

That is my two routers:
->addRoute('viewTextMaterial', new Zend_Controller_Router_Route(':mCat/:mCatSub/:mId/:mTitle', array('controller' => 'index', 'action' => 'viewtextmaterial')))
->addRoute('viewNews', new Zend_Controller_Router_Route(':nCat/:nId/:nTitle/:page', array('controller' => 'index', 'action' => 'viewnews')))
In index.phtml file I add this:
Test
Exp. for viewnews URL:
some text
But why, when I click a href, it redirect me to 'viewnews'?
In my experience(which is not very great :) )
I think when you use the colon in front of a name, when you are defining a router
i.e like
'/:mCat/:mCatSub/:mId/:mTitle',
array(
'controller' => 'index',
'action' => 'viewtextmaterial'
)
What you are telling the router to do is to route any url, which follows the above format('/:mCat/:mCatSub/:mId/:mTitle'), to be routed to the controller/action you mentioned there. eg.
someController/action/x/y
or
anoCont/act/a/b
would be routed to the same controller/action.
So in your case what you are doing is you are defining two routers with same options(which creates ambiguity), and by default the second defined route is used(Bottom to top matching).
you can use something like this
'/test/:mCatSub/:mId/:mTitle',
array(
'controller' => 'index',
'action' => 'viewtextmaterial'
)
so anything that starts with 'test' as controller(in the url) would now be routed to your desired controller/view.
Hope it works.. :) (If it doesn't please enlighten me :) )

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: Routing seems to interfere with rendering of Zend_Navigation menu

i have setup a route like below in bootstrap.php
$route = new Zend_Controller_Router_Route(
'users/:id',
array(
'controller' => 'users',
'action' => 'view'
)
);
$router->addRoute('viewUser', $route);
when i try to goto /users/1, and in view script do echo $page->getHref(), i get
Fatal error:
Zend_Controller_Router_Exception: id
is not specified in
D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\View\Helper\Navigation\HelperAbstract.php
on line 522
when i try to goto /users/view/id/1 it works ok. i am wondering why would rendering menu items from Zend_Navigation crash in something separate like routing?
ok i solved the problem with help from another post
basically, i need to add a default value for id
$route = new Zend_Controller_Router_Route(
'users/:id',
array(
'controller' => 'users',
'action' => 'view',
'id' => '0'
)
);
$router->addRoute('viewUser', $route);

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.