ThinkPHP5 路由绑定

来源:互联网 发布:unity3d中文版下载 编辑:程序博客网 时间:2024/06/06 03:45

第1种:

 // 绑定当前的URL到 index模块的index控制器

use think\Route;
Route::bind('index/Index'); // http://contoso.org/getLine/123


第2种:

 // 绑定当前的URL到 index模块的index控制器

use think\Route;
Route::bind('index/Index');

Route::get(':id','index/Index/getLine');  // http://contoso.org/125


第3种:

use think\Route;

// 绑定当前的URL到 index模块的index控制器的getLine操作

Route::bind('index/Index/getLine');  // http://contoso.org/127


第4种:

use think\Route;
// 绑定当前的URL到 index模块
Route::bind('index');
Route::get(':id','Index/getLine');  // http://contoso.org/129


第5种:

use think\Route;
//绑定到命名空间
//把当前的URL绑定到某个指定的命名空间,例如:
//   绑定命名空间
Route::bind('\app\index\controller','namespace');

// 例如:
// http://contoso.org/index/getAll/586  我们接下来只需要通过此链接就可以直接访问 \app\index\controller\Index类的getAll方法。
// http://contoso.org/blog/read/45      我们接下来只需要通过此链接就可以直接访问 \app\index\controller\Blog 类的read方法。


第6种:

use think\Route;
//绑定到类
//把当前的URL直接绑定到某个指定的类,例如:
//  绑定到类
Route::bind('\app\index\controller\Blog','class');
//那么,我们接下来只需要通过
//  http://contoso.org/read/55
//就可以直接访问 \app\index\controller\Blog 类的read方法。
//注意:绑定到命名空间和类之后,不会进行模块的初始化工作。




入口文件绑定
如果我们需要给某个入口文件绑定模块,可以使用下面两种方式:
常量定义
只需要入口文件添加BIND_MODULE 常量,即可把当前入口文件绑定到指定的模块或者控制器,例如:
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 绑定到index模块
define('BIND_MODULE','index');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
自动入口绑定
如果你的入口文件都是对应实际的模块名,那么可以使用入口文件自动绑定模块的功能,只需要在应用配置
文件中添加:
// 开启入口文件自动绑定模块
'auto_bind_module' => true,
当我们重新添加一个 public/demo.php 入口文件,内容和public/index.php 一样:
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
但其实访问 demo.php 的时候,其实已经自动绑定到了demo 模块。


<?php
namespace app\index\controller;
use think\cache\driver\Redis;
use think\Request;

class Index
{
    public function getAll(Redis $redis)
    {
        echo $redis->get('hi');
        echo 'getAll';
    }
    
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function addLine()
    {
        echo 'addLine';
    }
    
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function saveLine(Request $request,Redis $redis)
    {
        $redis->set('hi','moxi,moxi?');
        echo 'saveLine';
    }
    
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getLine(Redis $redis,$id)
    {
        echo $redis->get('hi');
        echo 'getLine'.' '.$id;
    }
    
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function editLine($id)
    {
        echo 'editLine'.' '.$id;
    }
    
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updateLine(Request $request,Redis $redis, $id)
    {
        $redis->set('hi','welcome to china again');
        echo 'updateLine'.' '.$id;
    }
    
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deleteLine(Redis $redis, $id)
    {
        $redis->rm('hi');
        echo 'deleteLine'.' '.$id;
    }
}


<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Blog extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'index';
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'create';
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'save';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function read($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'read'.' '.$blog_id;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function edit($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'edit'.' '.$blog_id;
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function update(Request $request, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'update'.' '.$blog_id;
    }

    /**
     * 删除指定资源
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function delete($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'delete'.' '.$blog_id;
    }
    
    public function miss()
    {
        echo 'Blog 路由方法不存在';
    }
}


原创粉丝点击