// 配置网站根域名
'url_domain_root' => 'contoso.org',
以上命令会依据build.php的文件自动生成2个模块代码
namespace app\admin\controller;
use think\Controller;
use think\Request;
class User extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'User index'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'User create'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 corp.contoso.org';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'User save'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 corp.contoso.org';
}
public function miss()
{
echo 'User 路由URL不存在'.'
';
echo '192.168.10.20 corp.contoso.org';
}
}
namespace app\blog\controller;
use think\Controller;
use think\Request;
class Blog extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'Blog index'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'Blog create'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 user.corp.contoso.org';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'Blog save'.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
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.'
';
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function miss()
{
echo 'Blog 路由URL不存在'.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
}
namespace app\blog\controller;
use think\Controller;
use think\Request;
class Comment extends Controller
{
public function create($blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is create, this is a Comment'.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function save(Request $request,$blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is save, this is a Comment'.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function read($id, $blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is read, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function edit($id, $blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is edit, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function update(Request $request, $id, $blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is update, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function delete($id, $blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is delete, this is a Comment'.' id ='.$id.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
public function index($blog_id)
{
echo $this->request->param('hi').'
';
echo $this->request->param('name').'
';
echo 'method is index, this is a Comment'.' blog_id='.$blog_id.'
';
echo '192.168.10.20 user.corp.contoso.org';
}
}
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)下面有效。