пятница, 23 декабря 2011 г.

Zend Framework, bind subdomains to modules

Params:
Zend Framework 1.11, PHP-5.3, Apache 2.2, local pc with Gentoo on board.

Problem:
I want to bind subdomains to certain module of Zend Framework application.
Example: 
http://mysite.com - follow to default module of my app.
http://admin.mysite.com - follow to admin module.
Controllers, actions and params may be various(global variables also):
http://admin.mysite.com/index/index/foo/bar/......
etc.

Solution:
1) Create site with default Zend Framework module structure.
/var/www/mysite.com/
   application/
      configs/
      modules/
         admin/
            controllers/
            views/
         default/
            ......
      Bootstrap.php
   ......

2) Bind DocumentRoot folder of mysite.com, www.mysite.com, admin.mysite.com to the same folder. In my case it's:
/var/www/mysite.com/public

3) In Bootstrap.php file init application router:
 protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$hostnameDefaultRoute = new Zend_Controller_Router_Route_Hostname(':module.mysite.com'  , array('module' => 'default'));
$router->addRoute('default', $hostnameDefaultRoute->chain(new Zend_Controller_Router_Route(':lang/:controller/:action/*', array('lang' => 'en', 'controller'=>'index', 'action'=>'index'))));
  $hostnameDefaultRoute = new Zend_Controller_Router_Route_Hostname('mysite.com'  , array('module' => 'default'));
$router->addRoute('www', $hostnameDefaultRoute->chain(new Zend_Controller_Router_Route(':lang/:controller/:action/*', array('lang' => 'ru', 'controller'=>'index', 'action'=>'index'))))
->addRoute('login'  , $hostnameDefaultRoute->chain(new Zend_Controller_Router_Route_Static('login',   array('controller' => 'user', 'action'=>'login'))));
// routes for admin area
$hostnameAdminRoute = new Zend_Controller_Router_Route_Hostname('admin.mysite.com', array('module' => 'admin'));
$router->addRoute('admin' , $hostnameAdminRoute->chain(new Zend_Controller_Router_Route(':lang/:controller/:action/*',   array('lang' => 'en', 'controller'=>'index', 'action'=>'index'))))
->addRoute('settings' , $hostnameAdminRoute->chain(new Zend_Controller_Router_Route_Static('settings', array('controller' => 'config', 'action' => 'list'))));
}
==================================================================
In a result will work this url's:
mysite.com/ -> Module: default, IndexController, indexAction, lang: en
mysite.com/en/index/index -> Module: default, IndexController, indexAction, lang: en
mysite.com/en/index/index/foo/bar -> Module: default, IndexController, indexAction, lang: en , params: foo = bar
mysite.com/login -> Module: default, UserController, loginAction
admin.mysite.com -> Module: admin, indexController, indexAction, lang: en
admin.mysite.com/en/ -> Module: admin, indexController, indexAction, lang: en
admin.mysite.com/settings -> Module: admin, configController, listAction

4) Also you can use route names in view helper URL. In view script:
echo $this->url(array(), 'www') will output 'mysite.com/en/index/index'
echo $this->url(array('foo' => 'bar'), 'www') will output 'mysite.com/en/index/index/foo/bar'
echo $this->url(array(), 'login') will output 'mysite.com/login'
echo $this->url(array('page' => 1), 'admin') will output 'admin.mysite.com/en/index/index/page/1'
echo $this->url(array(), 'settings') will output 'admin.mysite.com/settings'
Also you can specify controllers and actions
echo $this->url(array('controller' => 'post', 'action' => 'list'), 'www') will output 'mysite.com/en/post/list'
echo $this->url(array('action' => 'print', 'id' => 2), 'admin') will output 'admin.mysite.com/en/index/print/id/2'

Комментариев нет:

Отправить комментарий