ThinkPHP5 域名路由 - 09

[[email protected] ~]$ cat > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  contoso.org
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.20 user.corp.contoso.org
192.168.10.20 corp.contoso.org
192.168.10.20 contoso.org


[[email protected] ~]$ echo 'contoso.org' > /etc/hostname    //编辑文件设置主机名,等价命令 hostnamectl set-hostname contoso.org

[[email protected] ~]$ hostname
contoso.org
[[email protected] ~]$ cat > /etc/httpd/conf.d/httpd-vhosts.conf
<Directory "/home/myth/www/think">
        Options +Indexes +FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
</Directory>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/home/myth/www/think/public"
    ServerName contoso.org
    ServerAlias contoso.org
    ErrorLog "/home/myth/log/httpd/contoso-error_log"
    CustomLog "/home/myth/log/httpd/contoso-access_log" common
</VirtualHost>

<Directory "/home/myth/www/think">
        Options +Indexes +FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
</Directory>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/home/myth/www/think/public"
    ServerName corp.contoso.org
    ServerAlias corp.contoso.org
    ErrorLog "/home/myth/log/httpd/corp-contoso-error_log"
    CustomLog "/home/myth/log/httpd/corp-contoso-access_log" common
</VirtualHost>

<Directory "/home/myth/www/think">
        Options +Indexes +FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
</Directory>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/home/myth/www/think/public"
    ServerName user.corp.contoso.org
    ServerAlias user.corp.contoso.org
    ErrorLog "/home/myth/log/httpd/user-corp-contoso-error_log"
    CustomLog "/home/myth/log/httpd/user-corp-contoso-access_log" common
</VirtualHost>

[[email protected] ~]$


ThinkPHP5 域名路由 - 09

我们需要在config.php文件里修改配置参数

  // URL参数方式 0 按名称成对解析 1 按顺序解析
    'url_param_type'         => 1,
    // 是否开启路由
    'url_route_on'           => true,

// 启用域名部署路由功能,首先需要开启:
'url_domain_deploy' =>  true

虽然一般情况下系统会自动识别,不过为了稳妥起见,我们建议你配置下网站的根域名,尤其是当你的域名是国家后缀或者使用了三级子域名的时候,配置方式为:

// 配置网站根域名'url_domain_root'    =>  'contoso.org'


编程要首先创建2个模块 一个admin模块和一个blog模块,手动在apps文件夹下创建一个名称为 build.php的文件,代码如下:

<?php

return [

// 定义admin模块的自动生成
    'admin' => [
        '__dir__' => ['controller', 'model', 'view'],
        'controller' => ['User', 'UserType'],
        'model' => ['User', 'UserType'],
        'view' => ['index/index', 'index/admin'],
    ],

// 定义blog模块的自动生成
    'blog' => [
        '__dir__' => ['controller', 'model', 'view'],
        'controller' => ['Blog'],
        'model' => ['Blog'],
        'view' => ['index/index'],
    ],
];


在命令行控制台终端上执行:

[[email protected] ~]$ cd /home/myth/www/think && php think build

以上命令会依据build.php的文件自动生成2个模块代码


<?php
namespace app\admin\controller;

use think\Controller;

class User extends Controller
{
    public function read($id){
        echo '192.168.10.20 corp.contoso.org'.'  '.$id;
    }
}


<?php
namespace app\blog\controller;
use think\Controller;
class Blog extends Controller
{
    public function read($id){
        echo '192.168.10.20 user.corp.contoso.org'.'  '.$id;
    }
}

域名路由配置如下:

<?php
use think\Route;

// corp二级子域名绑定到admin模块
Route::domain('corp','admin');  // http://corp.contoso.org/user/read/120

// user.corp三级子域名绑定到blog模块
Route::domain('user.corp','blog'); // http://user.corp.contoso.org/blog/read/119


同一个项目,竟然可以使用2个以上的域名访问一个网站的不同模块,这就是使用域名路由的好处

访问路由URL的地址如下:

http://corp.contoso.org/user/read/120

http://user.corp.contoso.org/blog/read/119