Module As Subdomain In Zend Framework

In Zend Framework we can split an application into several modules. It purposed to make us easy to manage source code, and the source code become more structured.

In certain case, we need to make the module can be accessed as subdomain. In Zend Framework it can be solved with use Zend_Controller_Router_Route_Hostname. So the subdomain will be treat as a module, and Zend Framework will use existing source code in that module.

To use Zend_Controller_Router_Route_Hostname, make sure the subdomain have same DocumentRoot folder with domain. Because it will use same files, the different just the way to handle the request.

Then, make new module in application. Example, we want to make mobile module will be handled by mobile subdomain.

> zf create module mobile

Make IndexController in mobile module to handle request from mobile module.

> zf create controller Index index-action-included=1 mobile

Because of Zend_Controller_Router_Route is a part of available resource in Zend_Application_Resource, to make router configuration just need add some of following lines in application/configs/application.ini

resources.router.routes.mobile.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.mobile.route = ":module.domain"
resources.router.routes.mobile.defaults.module = "mobile"
resources.router.routes.mobile.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.mobile.chains.index.route = ":controller/:action/*"
resources.router.routes.mobile.chains.index.defaults.controller

Adjust ":module.domain" with domain name. If domain name is example.com, replace ":module.domain" with ":module.example.com". After that please try access the subdomain in web browser. To make sure that mobile module be used, pelase change content of application/modules/testrouter/views/scripts/index/index.phtml with text that identify that file inside mobile module, eg: "Mobile". So, when the configuration is success, web browser will display "Mobile".

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options