基于子域在CakePHP 3中设置默认前缀

基于子域在CakePHP 3中设置默认前缀,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我想构建多个子域,这些子域指向CakePHP v3的相同源代码 情况是 若域是“admin.localhost.com”,那个么前缀值应该是admin 如果域是“xyz.localhost.com”、“abc.localhost.com”或子域上的任何域,则前缀值应为vendor 若域是“localhost.com”或“www.localhost.com”,那个么前缀值应该是false,就像cakephp 3默认值一样 我试着从CakePHP3文档中查找数据。但我不知道如何设置默认前缀 提前谢谢

我想构建多个子域,这些子域指向CakePHP v3的相同源代码

情况是

  • 若域是“admin.localhost.com”,那个么前缀值应该是admin
  • 如果域是“xyz.localhost.com”、“abc.localhost.com”或子域上的任何域,则前缀值应为vendor
  • 若域是“localhost.com”或“www.localhost.com”,那个么前缀值应该是false,就像cakephp 3默认值一样
  • 我试着从CakePHP3文档中查找数据。但我不知道如何设置默认前缀


    提前谢谢

    我自己得到了问题的答案

    我们必须在
    config/routs.php
    中通过分解
    HTTP\u主机来设置前缀

    $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
    中通过分解
    HTTP\u主机来设置前缀

    $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');
        });
    }
    
    所以主要的技巧是需要在根作用域上传递前缀