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.
Related
Having hard time figuring out how to post a form to a plugin controller.
Say I have a reg/login plugin that could be shared with different apps.
in reg.ctp:
echo $this->Form->create(null, array('url'=>array('controller' => 'user', 'action' => 'submit','plugin'=>'user')));
I get missing controller.. in app/controller/user.php
what did do wrong?
Looks like your controller is not pluralised. All controllers in Cake, by convention, are plurals.
array('controller' => 'users', 'action' => 'submit', 'plugin' => 'user');
Should route to
app/Plugin/User/Controller/UsersController::submit()
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 );
I'm not sure how to fix this, or wat is the best way to approach this. Also couldn't find enough information to get me on the right way (could be that my searching sucks..)
Anyway, this is my problem:
I defined a route in my bootstrap file:
protected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/:controller/:action/:id/',
array('controller' => ':controller',
'action' => ':action',
'id' => ':id',
)
)
);
}
This works perfectly for my update and delete actions.
Now I've added the pagination to the indexpage. The pagination expects the page parameter. Because I haven't set this in my route, it cannot pass it, so my pagination doesn't work (as in switching between results).
I understand this. But what I want is that on the index page the id parameter isn't necessary and replace this with the page parameter.
Trying another route replacing id with page didn't work.
Is there a good way to solve this in the bootstrap or is it the best way to check for the action, and depending on the action, index or update/delete, define the route. The best place would than be a plugin?
Any advice or tips are greatly appreciated!
While working on another aspect of the application I came back to the same problem. I solved it, by specifying the routes much more.
First I deleted the $router->removeDefaultRoutes(); rule.
And then instead of (which didn't work):
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/:action/:page', array('controller' => ':controller', 'action' => ':action', 'page' => ':page'))
);
I now use this:
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/index/:page', array('controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);
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.
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.