您的位置:首页 > 编程语言 > PHP开发

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;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  路由定义