A look at how you can create custom routes and pass parts of your query string into a Zend_Controller as a named parameter.
Creating custom Zend_Controller routes
Description
A look at how you can create custom routes and pass parts of your query string into a Zend_Controller as a named parameter.
Tags
routing, zend_controller, zend_route

These are some of the best ZEND screencasts I’ve seen. Good work!
hey i tried using custom routes in my application like
$router->addRoute(
‘aed’,
new Zend_Controller_Router_Route(‘/:module/:controller/:action/:id’, array(‘module’ => ‘:module’, ‘controller’ => ‘:controller’, ‘action’ => ‘:action’, ‘id’ => “:id”))
);
but it does not work, as in when i try to go “/” it says
Invalid controller specified (:controller)
Request Parameters:
array(4) {
["module"]=>
string(7) “:module”
["controller"]=>
string(11) “:controller”
["action"]=>
string(7) “:action”
["id"]=>
string(3) “:id”
}
so i guess it removes all other routes?
Hi,
Ive followed the tutorial but having problems somewhere im my configuration.
I have everything the way it is in the videos, except when I type localhost/ it brings up the main page and if I want to access list.phtml or any others I need to type localhost/index/list
If I type localhost/index/jfejfoi I get an app error withing the template, however if I type localhost/list it gives a 404 outside of the application.
Do you know what I could be doing wrong?
Apparently apache was ignoring my .htaccess file which delt with the rewrite engine. So I configured a user.conf which set it right!
Another way to solve Rich’s problem is to edit the http.conf and change “AllowOverride None” to “AllowOverride All” ( you must restart Apache for this to take affect ). This will allow an .htaccess file to override the http.conf file. Another issue that I had was that when I typed in “http://localhost/index/index” or “http://localhost/index/list” or “http://localhost/list/user” the css was not being loaded. To solve this, I changed the “appendStylesheet” argument from “css/main.css” to “../css/main.css” ( along with all of the other css sheets ). I’m not sure if this has to do with my specific setup or not.
This was EXACTLY what I was looking for! Thanks so much for the very clear explanation. The docs do a good job of explaining and giving code samples but they never tell you where to put the code.
I’m using ZF 1.10 and I believe the framework has gone a long way since you have posted this. I believe the way in which some of the methods return values might have changed.
I think so because while running the code I was getting “User picked: Array”. On doing a var_dump($user) I noted it is indeed returning an array. So I had to change my code like this to get the code to work as you proposed
if($this->getRequest()->getParams(“users”)) {
$user = $this->getRequest()
->getParams(“users”);
if($user['users']) {
$this->view->user = “User picked : ” . $user['users'];
} else {
$this->view->user = “User not selected”;
}
}
Also the way bootstrapping works also changed in 1.10. I wrote the route in index.php inside public. I hope what I have understood is correct and you’ll give a clarification on the matter. Thanks
Hello there,
Great tutorial….. and….vous avez un bon accent en Français
.
Fabrice
Hey thanks for the tuts.
As of zf 1.10 i use a slightly different way of defining routes
Add the following to your application.ini file under application/configs
resources.router.routes.listOnIndex.route = /list
resources.router.routes.listOnIndex.defaults.module = default
resources.router.routes.listOnIndex.defaults.controller = index
resources.router.routes.listOnIndex.defaults.action = list
resources.router.routes.listOnIndexWithUsers.route = /list/:users
resources.router.routes.listOnIndexWithUsers.defaults.module = default
resources.router.routes.listOnIndexWithUsers.defaults.controller = index
resources.router.routes.listOnIndexWithUsers~.defaults.action = list
you will get the exact same result. Don’t know the difference between adding to the application.ini file vs the index.php ( refer Christy John’s comment)
Hope that helps!
whoops a quick edit to my last code:
change the last line from:
resources.router.routes.listOnIndexWithUsers~.defaults.action = list
to
resources.router.routes.listOnIndexWithUsers.defaults.action = list
Good Job!
Thanks from Brazil
Ok.. I’ve made in my bootstrap:
protected function _initRoute() {
$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
$router->addRoute(
‘userOnUsers’,
new Zend_Controller_Router_Route(‘/users/:user’, array(
‘module’ => ‘default’,
‘controller’ => ‘user’,
‘action’ => ‘users’
)
)
);
}
Now I have a database with users.. But what if the user in the url does not exist?
http://www.site.nl/users/this-user-does-not exist
Then I want a 404 message: Page Does Not Exist. How and where do I do that?
I am getting this error:
Fatal error: Call to a member function setViewSuffix() on a non-object in
when I navigate to http://localhost/list
any idea why?
Hi,
how i can get this url using router on application.ini
/comedie/la-fabrique-des-sentiments-streaming-261.html
( ps : /categorie/the-movie-name-id.html )
thank you