ThinkPHP5 路由定义 - 02
2017-07-27 17:58
363 查看
注册路由规则
路由注册可以采用方法动态单个和批量注册,也可以直接定义路由定义文件的方式进行集中注册。
动态注册
路由定义采用\think\Route 类的rule 方法注册,通常是在应用的路由配置文件
application/route.php 进行注册,格式是:
Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
例如注册如下路由规则:
use think\Route;
// 注册路由到index模块的News控制器的read操作
use think\Route;
Route::rule('news/create$','index/News/create','GET'); // create GET http://contoso.org/news/create
Route::rule('news$','index/News/save','POST'); // save POST http://contoso.org/news
Route::rule('news/:id/edit$','index/News/edit','GET',[],['id'=>'\d+']); // edit GET http://contoso.org/news/100/edit
Route::rule('news/:id$','index/News/read','GET',[],['id'=>'\d+']); // read GET http://contoso.org/news/100
Route::rule('news/:id$','index/News/update','PUT',[],['id'=>'\d+']); // update PUT http://contoso.org/news/100
Route::rule('news/:id$','index/News/delete','DELETE',[],['id'=>'\d+']); // delete DELETE http://contoso.org/news/100
Route::rule('news$','index/News/index','GET'); // index GET http://contoso.org/news
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class News extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'index';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'create';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'save';
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
echo 'read'.' '.$id;
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
echo 'edit'.' '.$id;
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
echo 'update'.' '.$id;
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
echo 'delete'.' '.$id;
}
}
路由注册可以采用方法动态单个和批量注册,也可以直接定义路由定义文件的方式进行集中注册。
动态注册
路由定义采用\think\Route 类的rule 方法注册,通常是在应用的路由配置文件
application/route.php 进行注册,格式是:
Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
例如注册如下路由规则:
use think\Route;
// 注册路由到index模块的News控制器的read操作
use think\Route;
Route::rule('news/create$','index/News/create','GET'); // create GET http://contoso.org/news/create
Route::rule('news$','index/News/save','POST'); // save POST http://contoso.org/news
Route::rule('news/:id/edit$','index/News/edit','GET',[],['id'=>'\d+']); // edit GET http://contoso.org/news/100/edit
Route::rule('news/:id$','index/News/read','GET',[],['id'=>'\d+']); // read GET http://contoso.org/news/100
Route::rule('news/:id$','index/News/update','PUT',[],['id'=>'\d+']); // update PUT http://contoso.org/news/100
Route::rule('news/:id$','index/News/delete','DELETE',[],['id'=>'\d+']); // delete DELETE http://contoso.org/news/100
Route::rule('news$','index/News/index','GET'); // index GET http://contoso.org/news
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class News extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
echo 'index';
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
echo 'create';
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
echo 'save';
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
echo 'read'.' '.$id;
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
echo 'edit'.' '.$id;
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
echo 'update'.' '.$id;
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
echo 'delete'.' '.$id;
}
}
相关文章推荐
- ThinkPHP5.1 数据库查询数据 定义淘宝风格的路由URL
- [ThinkPHP5.0] 2定义路由
- ThinkPHP5.1 闭包路由 直接输出数据 不需要定义控制器
- [thinkPHP5项目实战_02]命名空间的定义
- Thinkphp5中定义路由
- thinkphp路由重定义简化url访问地址
- thinkphp URL规则、URL伪静态、URL路由、URL重写、URL生成
- 2017_01_03_02_路由
- ThinkPHP5 路由
- Thinkphp5笔记九:路由设置,隐藏indx.php
- ThinkPHP的URL路由
- ThinkPHP定义模板常量并在模板中使用
- 【Web前端学习笔记】Javascript_02_运算符,控制语句,常用语句,函数定义
- 17 - 06 - 02 路由重分布
- ThinkPHP框架系统源码解析——URL路由解析
- ThinkPHP中的系统常量和预定义常量集合
- 【02-C语言】11-函数的声明和定义
- thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?
- thinkphp 路由实现列表和详情
- ThinkPhp 3.1 路由简单配置及URL规则