Zend_Navigation url generation issue - zend-framework

I use Navigation component for site menus. I also use let zend figure-out the selected menu item from request parameters - I guess this is done automatically. The only problem is, that for this to work, action and controller have to be specified in navigation configuration for every node. This also means that when zend generates links from route, action and controller information to appended to the generated link automatically.
Anyone had the same problem?
Zend manual section, explaining the Mvc navigation page features.
Example:
some route defined in bootstrap:
$router->addRoute('user_profile_tab', new Zend_Controller_Router_Route(
'profil/:user/:location/:tab/*',
array(
'action' => 'profile',
'controller' => 'user',
'user' => ($user ? $user->id : 0), //change later
'location' => 0 //inject appropriate value later
)
));
navigation container object:
$container = .....
......,
array(
'label' => tr('Privileges'),
'id' => 'user-profile-perms',
'type' => 'Zulu_Navigation_Page',
'controller' => 'user',
'action' => 'profile',
'route'=> 'user_profile_tab',
'params' => array('tab'=>Main_Lib_Common::NAVI_USER_TAB_PERMS)
)
);
the result when using
$page = $container->getById('user-profile-perms');
$page->href;
http://www.example.com/profil/1/0/3/controller/user/action/profile
WHY action and controler params in the navigation container object you ask. The $page->isActive() check needs this data to make a perfect match.
THE FIX:
extend mvc navigation page and provide an alternative getHref() method ... one that removes action, controller and module params when a route does not define them.

I have done this to fix this weird behaviour:
extend mvc navigation page
provide an alternative getHref() method
check for routes, not having action , controller and module parameters and remove them from params array before href generation.
This way the isActive matching will still work, as we didnt modify the route or navigation nodes in any way.

Related

How to get the variables in routing in zend framework 1.12

I am new to routing in zf. I don't understand some terms in route
$route = new Zend_Controller_Router_Route(
'author/:username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
How do we get :username here? from where do we get this?
Username here is parameter pass by the user. So correct link using this route will be http://somepage.com/author/John. In your controller you can get this variable just like POST and GET variables - $this->getParam('author');
If you want to allow user use link without parameter (default parameter) you can add to array - 'author' => null (i usually use this in pagination)

Zend router behaviour

I have some trouble with the router.
I have a custom route :
$router->addRoute('showTopic',
new Zend_Controller_Router_Route('/forum/topic/:topic',
array('module' => 'forum',
'controller' => 'topic',
'action' => 'show'),
array('topic' => '\d+')));
But when I try to access this url : localhost/forum/topic/16
I get this error :
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'topic is not specified'
But I don't want to put a default value for topic, because I also want the route /forum/topic to list all topics...
Secondly, I know that if I add a custom route, the default router is overridden, but I need to have some default routes too. The only way I have found is to set 'default' in the second parameter of the url view helper, like this
$this->url(array(
'module' => 'forum',
'controller' => 'topic',
'action' => 'add'
), 'default', true)
Is there a more elegant way instead of doing this for all url where I want to use the default behavior ?
You should have a default value for a topic and add the more general route (the one for forum/topic) after the more specific one. Route_Rewrite checks the routes beginning with the last one (it actually does an array_inverse).
The url helper delegates assembly urls to a route, its second paremeter being the name of the route to pull from the router. Since the default route is registered under the name of 'default', there is nothing really inelegant in using the name (it is not a magic string or a special case). If this really bugs you, you could write a custom helper (to be placed under "views/helpers"):
class Zend_View_Helper_DefaultUrl extends Zend_View_Helper_Abstract {
public function defaultUrl($params) {
return $this->view->url($params, 'default');
}
}
And use it in your view like defaultUrl(array('action'=>'test')) ?>.

What is Route, where and how to define it?

I have several modules in my zend application. On one of the view script of my modules, I created a URL as such
$links['create'] = $this -> url(array("controller" => "roles", "action" => "create"), "custom");
This brings an error, saying Route "custom" is not define.
What is Route? Where to define it and How?
In my bootstrap file i have initialized my routing by adding following function
public function _initRouting() {
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$routedetialevent = new Zend_Controller_Router_Route(
'/events/detail/:id',
array(
'controller' => 'events',
'action' => 'detail'
)
);
$routeregister = new Zend_Controller_Router_Route(
'/index/register/:id',
array(
'controller' => 'index',
'action' => 'register'
)
);
$routerdetail = new Zend_Controller_Router_Route(
'/commentaries/details/:id',
array(
'controller' => 'commentaries',
'action' => 'details'
)
);
$router->addRoute('post', $routedetialevent);
$router->addRoute('register', $routeregister);
$router->addRoute('detail', $routerdetail);
}
as i have added the custom route in my events, commentaries whenever i visit detail page i dont have to write id in my url so my url will be like
http://localhost/example/events/detail/3
If i wouldnt have added route than my url would be like
http://localhost/example/events/detail/id/3
The Zend Framework manual has pretty decent documentation about routes and the router, including descriptions of several ways to define routes.
At a very basic level, routes are used both to parse URLs into parameters (like which controller and action should be used), and to do the reverse: take parameters and produce a URL.
For your purposes, unless you want to change how ZF will build your URL, you can just drop the "custom" part off of your url call.

Zend Naviation Menu View Helper - All links are the same?

heres my setup:
http://www.example.com/<module>/<controller>/<action>
I have this route defined:
$router->addRoute(
'editUser', new Zend_Controller_Router_Route('admin/users/edit/:id',
array(
'module' => 'admin',
'controller' => 'users',
'action' => 'edit',
'route' => 'default',
'id' => 0,
),
array('id' => '\d+')
)
);
So my sites navigation menu works fine until i go to a page like so:
http://www.example.com/admin/users/edit/10
It displays the page no problem but now every link in the navigation menu points to http://www.example.com/admin/users/edit
Not sure why this is happening and would like to get it fixed while maintaining use of the router.
Thanks in advance for any pointers!
It's a common problem.
If you are using custom routes in your navigation (editUser in your case) you must explicitly pass 'default' route to Zend_Navigation pages and url() view helpers.

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.