ThinkPHP5 域名单独分组路由
来源:互联网 发布:数据库分析工具 编辑:程序博客网 时间:2024/05/21 20:25
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
[myth@contoso ~]$ echo 'contoso.org' > /etc/hostname //编辑文件设置主机名,等价命令hostnamectl set-hostname contoso.org
[myth@contoso ~]$ hostname
contoso.org
[myth@contoso ~]$ 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 zhengzizhi@126.com
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 zhengzizhi@126.com
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 zhengzizhi@126.com
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>
[myth@contoso ~]$
我们需要在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'],
],
];
在命令行控制台终端上执行:
[myth@contoso ~]$ cd /home/myth/www/think && php think build
以上命令会依据build.php的文件自动生成2个模块代码
<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
class User extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'User index'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'User create'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'User save'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 显示指定的资源
*
* @param int $user_id
* @return \think\Response
*/
public function read($user_id)
{
echo 'User read'.' '.$user_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 显示编辑资源表单页.
*
* @param int $user_id
* @return \think\Response
*/
public function edit($user_id)
{
echo 'User edit'.' '.$user_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $user_id
* @return \think\Response
*/
public function update(Request $request, $user_id)
{
echo 'User update'.' '.$user_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 删除指定资源
*
* @param int $user_id
* @return \think\Response
*/
public function delete($user_id)
{
echo 'User delete'.' '.$user_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 corp.contoso.org';
}
public function miss()
{
echo 'User 路由URL不存在'.'<br>';
echo '192.168.10.20 corp.contoso.org';
}
}
<?php
namespace app\blog\controller;
use think\Controller;
use think\Request;
class Blog extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'Blog index'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'Blog create'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'Blog save'.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 显示指定的资源
*
* @param int $blog_id
* @return \think\Response
*/
public function read($blog_id)
{
echo 'Blog read'.' '.$blog_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 显示编辑资源表单页.
*
* @param int $blog_id
* @return \think\Response
*/
public function edit($blog_id)
{
echo 'Blog edit'.' '.$blog_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $blog_id
* @return \think\Response
*/
public function update(Request $request, $blog_id)
{
echo 'Blog update'.' '.$blog_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 删除指定资源
*
* @param int $blog_id
* @return \think\Response
*/
public function delete($blog_id)
{
echo 'Blog delete'.' '.$blog_id.'<br>';
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function miss()
{
echo 'Blog 路由URL不存在'.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
}
<?php
namespace app\blog\controller;
use think\Controller;
use think\Request;
class Comment extends Controller
{
public function create($blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is create, this is a Comment'.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function save(Request $request,$blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is save, this is a Comment'.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function read($id, $blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is read, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function edit($id, $blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is edit, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function update(Request $request, $id, $blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is update, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function delete($id, $blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is delete, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
public function index($blog_id)
{
echo $this->request->param('hi').'<br>';
echo $this->request->param('name').'<br>';
echo 'method is index, this is a Comment'.' blog_id='.$blog_id.'<br>';
echo '192.168.10.20 user.corp.contoso.org';
}
}
<?php
use think\Route;
//域名单独分组路由
//子资源路由
//每个子域名可以单独注册路由而彼此不受影响,例如下面的方法给 user.corp三级子域名注册了单独的路由:
Route::domain('user.corp','blog'); // 第2个参数实际上指的就是模块名称, 因为都是操作blog模块------所以嵌套资源路由拥有共同的user.corp三级子域名
Route::group(['name'=>'blogs/:blog_id/comments','prefix'=>'blog/Comment/'], function() {
Route::get('create$','create',['merge_extra_vars'=>true]);
Route::post('/$','save',['merge_extra_vars'=>true]);
Route::get(':id/edit$','edit',['merge_extra_vars'=>true]);
Route::get(':id$','read',['merge_extra_vars'=>true]);
Route::put(':id$','update',['merge_extra_vars'=>true]);
Route::delete(':id$','delete',['merge_extra_vars'=>true]);
Route::get('/$','index',['merge_extra_vars'=>true]);
//嵌套资源分组路由内部子资源分组路由千万别使用miss路由,否则将导致父资源分组路由无法执行,
//但是miss路由可以写在根资源路由里面,根控制器方法里面需要定义miss方法,必须注释掉
// Route::miss('miss');如果发现路由URL地址与上述路由规则无法匹配,那么它会调用根资源里面定义的miss路由方法;
}, [], ['blog_id' => '\d+','id' => '\d+']);
// create GET http://user.corp.contoso.org/blogs/128/comments/create?hi=hello&name=jason
// save POST http://user.corp.contoso.org/blogs/128/comments?hi=hello&name=jason
// edit GET http://user.corp.contoso.org/blogs/128/comments/32/edit?hi=hello&name=jason
// read GET http://user.corp.contoso.org/blogs/128/comments/32?hi=hello&name=jason
// update PUT http://user.corp.contoso.org/blogs/128/comments/32?hi=hello&name=jason
// delete DELETE http://user.corp.contoso.org/blogs/128/comments/32?hi=hello&name=jason
// index GET http://user.corp.contoso.org/blogs/128/comments?hi=hello&name=jason
//父资源路由
Route::group(['name'=>'blogs','prefix'=>'blog/Blog/'], function() {
Route::get('create$','create',['merge_extra_vars'=>true]);
Route::post('/$','save',['merge_extra_vars'=>true]);
Route::get(':blog_id/edit$','edit',['merge_extra_vars'=>true]);
Route::get(':blog_id$','read',['merge_extra_vars'=>true]);
Route::put(':blog_id$','update',['merge_extra_vars'=>true]);
Route::delete(':blog_id$','delete',['merge_extra_vars'=>true]);
Route::get('/$','index',['merge_extra_vars'=>true]);
Route::miss('miss'); // 在根资源路由里面写miss路由
}, [], ['blog_id' => '\d+']);
// create GET http://user.corp.contoso.org/blogs/create?hi=hello&name=jason
// save POST http://user.corp.contoso.org/blogs?hi=hello&name=jason
// edit GET http://user.corp.contoso.org/blogs/100/edit?hi=hello&name=jason
// read GET http://user.corp.contoso.org/blogs/100?hi=hello&name=jason
// update PUT http://user.corp.contoso.org/blogs/100?hi=hello&name=jason
// delete DELETE http://user.corp.contoso.org/blogs/100?hi=hello&name=jason
// index GET http://user.corp.contoso.org/blogs?hi=hello&name=jason
//每个子域名可以单独注册路由而彼此不受影响,例如下面的方法给 corp二级子域名注册了单独的路由:
Route::domain('corp','admin'); // 第2个参数实际上指的就是模块名称
Route::group(['name'=>'users','prefix'=>'admin/User/'], function() {
Route::get('create$','create',['merge_extra_vars'=>true]);
Route::post('/$','save',['merge_extra_vars'=>true]);
Route::get(':user_id/edit$','edit',['merge_extra_vars'=>true]);
Route::get(':user_id$','read',['merge_extra_vars'=>true]);
Route::put(':user_id$','update',['merge_extra_vars'=>true]);
Route::delete(':user_id$','delete',['merge_extra_vars'=>true]);
Route::get('/$','index',['merge_extra_vars'=>true]);
Route::miss('miss'); // 在根资源路由里面写miss路由
},[],['user_id'=>'\d+']);
// create GET http://corp.contoso.org/users/create?hi=hello&name=jason
// save POST http://corp.contoso.org/users?hi=hello&name=jason
// edit GET http://corp.contoso.org/users/100/edit?hi=hello&name=jason
// read GET http://corp.contoso.org/users/100?hi=hello&name=jason
// update PUT http://corp.contoso.org/users/100?hi=hello&name=jason
// delete DELETE http://corp.contoso.org/users/100?hi=hello&name=jason
// index GET http://corp.contoso.org/users?hi=hello&name=jason
//闭包函数中可以注册任意的路由(包括分组、别名、资源以及高级用法),但只会在指定的域名(或者IP)下面有效。
- ThinkPHP5 域名单独分组路由
- ThinkPHP5 域名单独路由
- ThinkPHP5 域名路由
- ThinkPHP5 路由
- ThinkPHP5 单独运行某个文件
- ThinkPHP5 路由模式
- ThinkPHP5 路由定义
- ThinkPHP5 批量注册路由
- ThinkPHP5 路由变量规则
- ThinkPHP5 路由绑定
- ThinkPHP5 批量注册路由
- ThinkPHP5 资源嵌套分组路由配置方法(可以根据需要实现多层嵌套,注意:资源嵌套分组路由配置顺序依次是嵌套层次递减)
- [ThinkPHP5.0] 2定义路由
- ThinkPHP5.0 学习心得------配置&路由
- ThinkPHP5.0---------URL和路由
- ThinkPHP5.1 多级控制器路由
- ThinkPHP5.1 路由重定向
- 路由分组转发算法
- Uinux命令一
- jQuery选择器——属性过滤选择器
- 堆排序
- 学习笔记-面向对象思想
- TensorFlow基础知识2-张量
- ThinkPHP5 域名单独分组路由
- Class类
- 32. Longest Valid Parentheses
- MySQL中子查询IN,EXISTS,ANY,ALL,SOME,UNION介绍
- $.ajax中contentType: “application/json” 的用法
- 网络搭建
- python爬虫学习第八天
- 【CodeForces
- Java基础 —— 原生数据类型与包装类