Set multiple router for same controller in zend framework - zend-framework

How to set multiple router for the same controller,if we are facing with the different action in one controller?
I have two action in my controller services in admin module.
First action is manage and second is manageArticle
Here is my code
protected function _initRoutes(){
$this->bootstrap('FrontController');
$router = $this->getResource('FrontController')->getRouter();
$route = new Zend_Controller_Router_Route(
'admin/services/:actionType',
array('module' => 'admin',
'controller' => 'services',
'action' => 'manage'),
array('actionType' => '(add|edit)')
);
$router->addRoute('services', $route);
$routeServiceArticle = new Zend_Controller_Router_Route(
'admin/services/article/:actionType',
array('module' => 'admin',
'controller' => 'services',
'action' => 'manageArticle'),
array('actionType' => '(addArticle|editArticle)')
);
$router->addRoute('services', $routeServiceArticle);
}
Please help me
Thanks in advance!!!

You need to give the routes different names, e.g.:
$router->addRoute('services', $route);
[...]
$router->addRoute('servicesArticle', $routeServiceArticle);
Then it should work.

Related

Route sudmains controller with Zend

I have the following structure on my app:
Modules =>
default => site.com
blog => blog.site.com
admin => admin.site.com
I used this code on my bootstrap to allow subdomains and redirect to the follow module:
$pathRoute = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$blogDomainRoute = new Zend_Controller_Router_Route_Hostname(
'blog.site.com', array(
'module' => 'blog',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('blogdomain', $blogDomainRoute->chain($pathRoute));
And the same code to adminDomainRoute.
It works fine! But now i notice that my pagination route don't work, i have the follow route to manage pages in admin module:
routes.usuarios.route = /usuarios/pagina/:pagina
routes.usuarios.defaults.module = admin
routes.usuarios.defaults.controller = usuarios
routes.usuarios.defaults.action = index
routes.usuarios.defaults.pagina = 1
I tried to change the route to
routes.usuarios.route = admin.site.com/usuarios/pagina/:pagina
But i still got action no found:
array (
'controller' => 'usuarios',
'action' => 'pagina',
'module' => 'admin',
)
How can i route admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3 ?
The thing that jumps at me from your setup is, that in the ini format (your current admin route), you are using the default router. Well this router knows nothing about the hostname you are on, so it is looking for an url like this one:
site.com/admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3
What you want is something like this:
//Create a hostname route. This route is only concerned with the subdomain part of the uri
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
'admin.:host.:domain');
//Create a default router that would take care of the rest of the routing.
$defaultRoute = new Zend_Controller_Router_Route(
'/usuarios/pagina/:pagina',
array(
'module'=>'admin',
'controller'=>'usuarios',
'action'=>'index'
)
);
//Chain those two routes together to make them go one after the other.
$defaultRoute=$hostnameRoute->chain($defaultRoute);
This code may need a bit of tweaking, but I think this should do what you need.

ZF Frontend and backend routing

I want something that sounds fairly simple to me but appears not to be.
My problem is that I need 2 routes voor my application:
Whenever the module is admin apply the following route:
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
Which works great. An example url could be: http://localhost/server/domains/demo/admin/images/album/3 where admin is the module, images the controller and so on.
All I want is that when a user goes to http://localhost/server/domains/demo he is redirected to the default module, index controller and index action. Everything after demo/ should be considered a single parameter (with unknown / possible).
I tried several things, from using Route_Regex, trying (.*) or (\d+), things I found all around online. Tried switching values, making them static, turning on/off removeDefaultRouter, but nothing worked. Below you can see my current bootstrap. Any ideas?
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/admin/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
$router->addRoute(
'frontend',
new Zend_Controller_Router_Route('/default/:controller/:action/(.*)', array('module' => 'default', 'controller' => 'index', 'action' => 'index'))
);
Backend works fine, but whenon the http://localhost/server/domains/demo/ I get the following error: No route matched the request
When given an answer, please explain why, because Zend_Route has always been a little vague for me. Thanks in advance!
Temp fix
Below the temporary fix that I use. It works exactly how I want, but I still believe that the same is achievable with Zend_Route without checking if the module is admin.
$router = $this->frontController->getRouter();
$uri = explode('demo/', $_SERVER['REQUEST_URI']);
$uri = (isset($uri[1])) ? explode('/', $uri[1]) : $uri[0];
if($uri[0] == 'admin')
{
$route = new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => null, 'value' => null));
$router->addRoute('router', $route);
}
else
{
$route = new Zend_Controller_Router_Route('/*', array('module' => 'default', 'controller' => 'index', 'action' => 'index'));
$router->addRoute('router', $route);
}
It is worth doing if you really have to use this routes. For normal usage you can just generate links by
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'admin'));
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'default'));
and so on.
If you are not ok with this, try routes chaining:
$router = Zend_Controller_Front::getInstance()->getRouter();
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$request = Zend_Controller_Front::getInstance()->getRequest();
$frontRoute = new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request);
$backRoute = new Zend_Controller_Router_Route_Module(array('module' => 'admin'), $dispatcher, $request);
$route = new Zend_Controller_Router_Route('admin');
$router->addRoute('backend', $route->chain($backRoute));
$route = new Zend_Controller_Router_Route('default');
$router->addRoute('frontend', $route->chain($frontRoute));
Explenation:
Zend_Controller_Router_Route_Module is for definining modules routes for the application. By chaining it to normal route like new Zend_Controller_Router_Route('admin') you made links like /admin/[your_module_route] where [your_module_route] have defined defaults array('module' => 'admin') and can take other parameters too.
new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request); is defined as an default route on the standard router.

How do add a route in Zend with a module name

I want to add a route in my Zend Framework application.
I want a route like this:
example.com/modulename/titleofthearticle-12
who redirect to:
example.com/modulename/article/index/id/12
I made this code, but I don't know how to add the module name in the route:
$router = $front_controller->getRouter();
$route = new Zend_Controller_Router_Route_Regex(
'modulename/[a-z\-]*-([0-9]*)',
array('controller' => 'article', 'action' => 'index'),
array(1 => 'id')
);
$router->addRoute('article', $route);
How to add this module name in the route ?
Thanks !
Try:
$router = $front_controller->getRouter();
$route = new Zend_Controller_Router_Route_Regex(
'modulename/[a-z\-]*-([0-9]*)',
array(
'module' => 'modulename',
'controller' => 'article',
'action' => 'index'),
array(
1 => 'id'
)
);
$router->addRoute('article', $route);

Zend_Router_Regex and additional parameter

I have no idea how realize it.
I have this router
$route = new Zend_Controller_Router_Route_Regex(
'category/([-\w]+)(?:/(\d+))?',
array('module' => 'dynamic', 'controller' => 'index', 'action' => 'show-category'),
array(1 => 'category', 2 => 'pageNumber'),
'category/%s/%d'
);
$router->addRoute('dynamic-categories', $route);
Assembled url in view will be /category/news/1 by using $this->url(...) helper. I want that my first page of news will be /category/news. Should I use other route to do it or use router chaining. I'm frustrated. Thank you for help.
You can use Zend_Controller_Router_Route for straight forward mapping:
new Zend_Controller_Router_Route(
'/category/:category/:page',
array(
'module' => 'dynamic',
'controller' => 'index',
'action' => 'show-category',
'category' => 'news',
'page' => 1
)
)
Will match /category/, /category/news/, /category/othercategory or /category/news/4 (examples only of course).

How do you make a subdomain route for every action in a Zend Framework controller?

Using Zend Framework I want to use an account name (:account) as the subdomain that will call the basket controller. When using getParams() for the index action it does display the :account parameter but this does not work on any other actions in the basket controller.
This is the code I currently have in the bootstrap:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$domain_name = 'domain.com';
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':account.' . $domain_name,
array(
'controller' => 'basket'
)
);
$router->addRoute('account', $hostnameRoute->chain($plainPathRoute));
Thanks in advance for your help and guidance.
You actually need two chained routes:
Your subdomain route
A route responsible for everything after your domain
I use this config to set up a route this (although I map all subdomains to modules, but i guess you can fix this ;):
'subdomain' => array(
'type' => 'Zend_Controller_Router_Route_Hostname',
'route' => ':module.localhost',
'chains' => array(
'index' => array(
'type' => 'Zend_Controller_Router_Route',
'route' => ':controller/:action/*'
)
)
)