2016-08-01 81 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是需要通过在根范围前缀。