Zend Framework 2 Routing Subdomains to Module

Zend Framework 2 Routing subdomains to module

Zend Framework 2 doesn't have a notion of routing to modules; all routing mappings are between a URI pattern (for HTTP routes) and a specific controller class. That said, Zend\Mvc provides an event listener (Zend\Mvc\ModuleRouteListener) which allows you to define a URI pattern that maps to multiple controllers based on a given pattern, and so emulates "module routing". To define such a route, you would place this as your routing configuration:

'router' => array(
'routes' => array(
// This defines the hostname route which forms the base
// of each "child" route
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This Segment route captures the requested controller
// and action from the URI and, through ModuleRouteListener,
// selects the correct controller class to use
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),
),
),

(Click here to see an example of this @ ZendSkeletonApplication)

This is only half of the equation, though. You must also register every controller class in your module using a specific naming format. This is also done through the same configuration file:

'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),

The array key is the alias ModuleRouteListener will use to find the right controller, and it must be in the following format:

<Namespace>\<Controller>\<Action>

The value assigned to this array key is the fully-qualified name of the controller class.

(Click here to see an example of this @ ZendSkeletonApplication)

NOTE: IF you aren't using ZendSkeletonApplication, or have removed it's default Application module, you will need to register the ModuleRouteListener in one of your own modules. Click here to see an example of how ZendSkeletonApplication registers this listener

Zend Framework 2 - Multiple sub domains cause problems

This happens because your route names are the same. I would try a-ads and b-ads for route names and that should resolve your situation.

In the end the configuration is getting merged together. So it's like an array, when the last array is merged it overwrites anything before it.

Zend Framework: set Module as Subdomain

I had a similar problem using wildcard subdomains on a local enviroment. I was sure of my routing however the hosts file didin't contain the subdomain. This worked for me

127.0.0.1  mydomain.local
127.0.0.1 subdomain.mydomain.local

Adding sub domain based routes in Zend framework

Hi after much browsing in the web I came up with this solution for my problem

resources.router.routes.www.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.www.route = ":module.findchennai.com"
resources.router.routes.www.defaults.module = "www"
resources.router.routes.www.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.www.chains.index.route = ":controller/:action/*"
resources.router.routes.www.chains.index.defaults.controller = "index"
resources.router.routes.www.chains.index.defaults.action = "index"

The above code maps the module with sub domain

resources.router.routes.news.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.news.route = "news.findchennai.com"
resources.router.routes.news.defaults.module = "news"

resources.router.routes.edu.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.edu.route = "education.findchennai.com"
resources.router.routes.edu.defaults.module = "education"

resources.router.routes.edu.chains.list.type = "Zend_Controller_Router_Route"
resources.router.routes.edu.chains.list.route = ":categ/:page"
resources.router.routes.edu.chains.list.defaults.controller = "index"
resources.router.routes.edu.chains.list.defaults.action = "category"
resources.router.routes.edu.chains.list.defaults.page = 1

resources.router.routes.news.chains.list.type = "Zend_Controller_Router_Route"
resources.router.routes.news.chains.list.route = ":categ/:page"
resources.router.routes.news.chains.list.defaults.controller = "index"
resources.router.routes.news.chains.list.defaults.action = "category"
resources.router.routes.news.chains.list.defaults.page = 1

This solves the problem I faced and now could map correctly to the following urls

http://news.mysite.com/27-08-09/sample.html
http://education.mysite.com/27-08-09/sample.html

Still if some one knows how to optimise the above code further, Please let me know.

How do I write Routing Chains for a Subdomain in Zend Framework in a routing INI file?

Here's basically what you want, in INI format:

routes.b2b.type = "Zend_Controller_Router_Route_Hostname"
routes.b2b.route = "sales.sitename.com"
; you could specify a default module (or anything) to use for the whole
; route chain here, like so:
; routes.b2b.defaults.module = "default"

routes.b2b.chains.signup.type = "Zend_Controller_Router_Route_Static"
routes.b2b.chains.signup.route = "/signup"
routes.b2b.chains.signup.defaults.controller = "index"
routes.b2b.chains.signup.defaults.action = "signup"

routes.b2b.chains.anotherroute.route = "/something/:foo" ; etc, etc.
routes.b2b.chains.anotherroute.defaults.action = "foo"
routes.b2b.chains.anotherroute.defaults.controller = "index"
routes.b2b.chains.anotherroute.defaults.foo = "bar"
routes.b2b.chains.anotherroute.reqs.foo = '[a-z]+'

This will give you the following routes: b2b-signup, and b2b-anotherroute.

Here's some important notes on route chaining:

When chaining routes together, the parameters of the outer route have a higher priority than the parameters of the inner route. Thus if you define a controller in the outer and in the inner route, the controller of the outer route will be selected.

Parent / child chained route names are always concatenated with a dash! So, like in the example above, b2b.chains.signup becomes a route named b2b-signup (which you can use for URL assembly, etc).

You can keep chaining! Chains of chains can have chains.

Children of chained routes do not work with wildcards. See #ZF-6654. Here's blog post that talks about why that may not be a big deal.



Related Topics



Leave a reply



Submit