2016-08-01 79 views
0

我想建立多個子域名,其指向的CakePHP V3設置默認前綴CakePHP中3

場景的相同的源代碼是

  1. 如果域名是「admin.localhost.com 「那麼前綴值應該是admin。
  2. 如果域名爲「xyz.localhost.com」,「abc.localhost.com」或任何子域名,則前綴值應爲供應商
  3. 如果域名爲「localhost.com」或「www.localhost.com 「那麼前綴值應該是false,因爲cakephp 3默認爲false。

我試着從CakePHP 3文檔中找出。但我didint得到如何設置默認前綴。

由於提前

回答

3

我得到了我的問題的答案我自己

我們有爆炸的方式HTTP_HOST

$exp_domain= explode(".",env("HTTP_HOST")); 

$default_prefix=false; // default prefix is false 
if(count($exp_domain)>2 && $exp_domain[0]!="www") 
{ 
    if($exp_domain[0]=="admin") $default_prefix="admin"; 
    else $default_prefix="vendor"; 
} 

if($default_prefix=="admin") 
{ 
    // default routes for vendor users with base scope and pass prefix as admin ($default_prefix) 
    Router::scope('/', function ($routes) use($default_prefix) { 
     $routes->connect('/', ['controller' => 'admins', 'action' => 'dashboard','prefix'=>$default_prefix]); 
     $routes->connect('/:action', ['controller' => 'admins','prefix'=>$default_prefix]); 
     $routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]); 
     $routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]); 

    }); 

} 
else if($default_prefix=="vendor") 
{ 
    // default routes for vendor users with base scope and pass prefix as vendor ($default_prefix) 
    Router::scope('/', function ($routes) use($default_prefix) { 
     $routes->connect('/', ['controller' => 'vendors', 'action' => 'dashboard','prefix'=>$default_prefix]); 
     $routes->connect('/:action', ['controller' => 'vendors','prefix'=>$default_prefix]); 
     $routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]); 
     $routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]); 
    }); 
} 
else 
{ 
    // default routes for normal users with base scope 
    Router::scope('/', function ($routes) use($default_prefix) { 
     $routes->connect('/', ['controller' => 'users', 'action' => 'dashboard'); 
     $routes->connect('/:action', ['controller' => 'users'); 
     $routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action'); 
     $routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action'); 
    }); 
} 

所以,主要的竅門設置前綴config/routs.php是需要通過在根範圍前綴。