一、实现目标:
1.普通用户通过www.demo.com访问前台index模块
2.管理员通过admin.demo.com访问后台admin模块

二、实现步骤(windows+phpstudy环境下)
1.Nginx配置
server {

listen 80;
server_name www.demo.com;
root "E:/pro/a/public";

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

location / {
    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php?s=/$1 last;
}
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO  $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    include fastcgi_params;
}

location ~ /\.(?!well-known).* {
    deny all;
}

}

server {

listen 80;
server_name admin.demo.com;
root "E:/pro/a/public";

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm admin.php;

charset utf-8;

location / {
    if (!-e $request_filename) {
    rewrite ^(.*)$ /admin.php?s=/$1 last;
}
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO  $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    include fastcgi_params;
}

location ~ /\.(?!well-known).* {
    deny all;
}

}

当然还有hosts文件:
127.0.0.1 www.demo.com
127.0.0.1 admin.demo.com

2.入口
复制一份public下的index.php,重命名为admin.php,与index.php一样放在public目录下,作为admin.demo.com的入口

3.配置文件,入口自动绑定对应模块(入口文件名与模块名称一致,如demo.php对应application/demo模块名demo)
config/app.php
// 是否支持多模块

'app_multi_module'       => true,
// 入口自动绑定模块
'auto_bind_module'       => true,

4.问题发现
访问发现,通过域名admin.demo.com访问admin模块时url中模块名可省略,index模块url中省略index则会导致控制器被判断为模块从而报错(模块不存在),通过阅读thinkphp源码发现,thinkApp.php中:

  if ($this->bindModule) {
            // 模块/控制器绑定
            $this->route->bind($this->bindModule);
        } elseif ($this->config('app.auto_bind_module')) {
            // 入口自动绑定
            $name = pathinfo($this->request->baseFile(), PATHINFO_FILENAME);//入口文件名(不含后缀.php)
           
            //入口名为真
            if ($name && 'index' != $name && is_dir($this->appPath . $name)) {
                $this->route->bind($name);                   
            }
        }

模块名为index时,入口也应添加绑定模块的操作(bind)
// 执行应用并响应
Container::get('app')->bind('index')->run()->send();

三、一些实现目标的其他想法
1、单一入口,入口文件通过switch/if……elseif语句结合当前访问域名动态设置该入口绑定的模块
2、通过域名路由实现

标签: none

添加新评论