Creating custom Zend_Controller routes
January 21st, 2009A look at how you can create custom routes and pass parts of your query string into a Zend_Controller as a named parameter.
A look at how you can create custom routes and pass parts of your query string into a Zend_Controller as a named parameter.
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