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

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/*'
)
)
)

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.

Set multiple router for same controller in 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.

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.

Rewrite url in social engine

I am using social engine, I want to change my url from www.example.com/signup to www.example.com/activate, sorry I want to keep both urls. please let me know how to do this
Put this code in your public/index.php file.
$FrontController = Zend_Controller_Front::getInstance();
$Router = $FrontController->getRouter();
$Router->addRoute("activate",
new Zend_Controller_Router_Route
(
"/activate",
array
("controller" => "signup",
"action" => "index"
)
));
here: activate is the name of the router. /activate is the url you want as address, controller and action is self descriptive.
In application/modules/Use/settings/manifest find these lines:
'user_signup' => array(
'route' => '/signup/:action/*',
'defaults' => array(
'module' => 'user',
'controller' => 'signup',
'action' => 'index'
)
Change /signup/:action/ to /activate/:action.
You could modify your .htaccess to create redirects, refer to this example

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.