简体   繁体   English

根据子域在CakePHP 3中设置默认前缀

[英]Set Default Prefix in CakePHP 3 based on sub domain

I want to build multiple sub domain which point to same source code of CakePHP v3 我想构建多个指向CakePHP v3相同源代码的子域

Scenario is 场景是

  1. If domain is "admin.localhost.com" then prefix value should be admin. 如果domain为“ admin.localhost.com”,则前缀值应为admin。
  2. If domain is "xyz.localhost.com",'abc.localhost.com' or any on sub domain then prefix value should be vendor 如果域是“ xyz.localhost.com”,“ abc.localhost.com”或子域中的任何子域,则前缀值应为供应商
  3. If domain is "localhost.com" or "www.localhost.com" then prefix value should be false as cakephp 3 have by default. 如果domain是“ localhost.com”或“ www.localhost.com”,则前缀值应为false,因为cakephp 3默认情况下具有。

I have tryied to findout from CakePHP 3 document. 我试图从CakePHP 3文档中查找。 but I didint get how to set default prefix. 但我didint得到如何设置默认前缀。

Thanks in Advance 提前致谢

I got Answer of my question myself 我自己回答了我的问题

We have to set prefix in config/routs.php by exploding HTTP_HOST 我们必须通过config/routs.php HTTP_HOSTconfig/routs.php设置前缀

$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');
    });
}

So main trick is need to pass prefix on root scope. 因此,主要技巧是需要在根作用域上传递前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM