Route sudmains controller with Zend

84 views Asked by At

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 ?

1

There are 1 answers

2
Janis Peisenieks On BEST ANSWER

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.