IT俱乐部 PHP thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成

thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成

本文详细介绍了ThinkPHP5.0的路由功能,包括路由的作用、入口文件配置、路由模式(普通、混合、强制)、路由设置方法(动态单个注册、动态批量注册、配置文件批量注册)、变量规则、路由参数、资源路由的声明和自动注册规则,以及快捷路由的声明和控制器使用。此外,还讲解了如何生成URL以及隐藏入口文件的设置。

一、路由的作用

简化URL地址,方便记忆有利于搜索引擎的优化

二、入口文件

前后台分离

在网站public目录下(项目public)新建admin.php

打开admin.php

<?php // +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st 
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

绑定模块

实现功能
index.php 这个入口文件,只能去前台模块
admin.php这个入口文件,只能去后台模块(建议后台入口文件复杂一些)

如何实现
在入口文件中

// 定义前台
define('BIND_MODULE', 'index'); 
// 绑定后台
define('BIND_MODULE', 'admin');

URL地址发生变化
入口绑定之前(http://www.tp.com/admin.php/模块/控制器/方法)
入口绑定之后(http://www.tp.com/admin.php/控制器/方法)

隐藏入口文件

开启apache重写(D:wamp64binapacheapache2.4.23confhttpd.conf)
把注释开启 LoadModule rewrite_module modules/mod_rewrite.so

设置访问权限(D:wamp64binapacheapache2.4.23confextrahttpd-vhosts.conf)

    ServerName www.tp.com
    DocumentRoot D:/wamp64/www/study/thinkphpstudy/public
    
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    

入口文件,在网站public目录下新建.htaccess文件,

  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

重启服务

url地址变化
隐藏之前http://www.tp.com/index.php/控制器/方法
隐藏之后http://www.tp.com/控制器/方法

三、tp5.0路由学习注意

支持三种方式的url解析规则路由只针对应用,不针对模块,因此路由的设置也是针对应用下的所有模块。

关闭后台模块,在后台入口文件中(admin.php),写在加载框架引导文件之后,否则报错。

<?php // +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st 
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 绑定后台
define('BIND_MODULE', 'admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

// 关闭admin模块的路由
thinkApp::route(false);

路由的三种模式

普通模式

1.定义
关闭路由,完全使用默认的PATH_INFO方式URL:2.形式
http://www.tp.com/admin.php/index/index

3.如何设置

     // 是否开启路由
    'url_route_on'           => false,
    // 是否强制使用路由
    'url_route_must'         => false,

混合模式

1.定义
开启路由,并使用路由定义+默认PATH_INFO方式的混合

2.如何设置

     // 是否开启路由
    'url_route_on'           => true,
    // 是否强制使用路由
    'url_route_must'         => false,

强制模式

1.定义
开启路由,并设置必需定义路由才能访问

2.如何设置

     // 是否开启路由
    'url_route_on'           => true,
    // 是否强制使用路由
    'url_route_must'         => true,

四、设置路由

1.动态单个注册

设置路由格式

Route::rule(‘路由表达式’, ‘路由地址’, ‘请求类型’, ‘路由参数(数组)’, ‘变量规则(数组)’)

设置路由文件(项目applicationroute.php)

如何设置(route.php)

use thinkRoute;
// 定义路由规则
// 设置路由之后,就不能使用pathinfo访问了
Route::rule('/','index/index/index');
//注册路由访问到index模块下的index控制器下的index的方法
Route::rule('test','index/index/test');
//注册路由test 访问到index模块下的index控制器下的test的方法

路由的形式
1、静态地址路由

//注册路由test 访问到index模块下的index控制器下的test的方法
Route::rule('test','index/index/test');

2、给路由带参数

route.php中
//注册带参数路由
//http://www.tp.com/course/1
//http://www.tp.com/index/index/index/id/1
Route::rule('course/:id','index/index/course');
index.php中
function course(){
            return input('id');
        }

3、给路由带多个参数(设置了带几个就必需带几个)

route.php中
//注册带参数路由
//http://www.tp.com/time/1/2
//http://www.tp.com/index/index/shijian/year/1/month/2
Route::rule('time/:year/:month','index/index/shijian');
index.php中
function shijian(){
        return input('year').input('month');
    }

4、可选参数路由

route.php中
//注册带可选参数路由
//http://www.tp.com/time/1
//http://www.tp.com/index/index/shijian/year/1
Route::rule('time/:year/[:month]','index/index/shijian');
index.php中
function shijian(){
        return input('year').input('month');
    }

5、全动态路由(不建议使用)

    route.php中
    //注册带可选参数路由
    //http://www.tp.com/1/1
    //http://www.tp.com/index/index/dongtai/1/1
    Route::rule(':a/:b','index/index/dongtai');
    index.php中
    function dongtai(){
        return input('a').input('b');
    }

6、完全匹配路由

    route.php中
    //注册带可选参数路由
    //http://www.tp.com/1/1
    //http://www.tp.com/index/index/dongtai/1/1
    Route::rule(':a/:b$','index/index/dongtai');
    index.php中
    function dongtai(){
        return input('a').input('b');
    }

7、带额外参数

    route.php中
    // 带额外参数
    Route::rule('test2','index/index/test2?id=10&name=tian');
    index.php中
    function test2(){
        dump(input());
    }

设置请求类型
1.TP中请求类型
get,post,put,delete
2.Route::rule() 默认支持所有类型
3.设置各种请求

// 支持get请求的两种方式
Route::rule('type','index/index/type','get');
Route::get('type','index/index/type');
// 支持post请求的两种方式
Route::rule('type','index/index/type','post');
Route::post('type','index/index/type');
// 同时支持get和post
Route::rule('type','index/index/type','get|post');
// 支持所有路由
Route::rule('type','index/index/type','*');
Route::any('type','index/index/type' );
// 支持put请求
Route::rule('type','index/index/type','put');
Route::put('type','index/index/type' );
// 支持delete请求
Route::rule('type','index/index/type','delete');
Route::delete('type','index/index/type' );

4.如何模拟put和delete请求


2.设置路由-动态批量注册

1.基本格式

    Route::rule([
        '路由规则1'=>'路由地址和参数',
        '路由规则2'=>['路由地址和参数','匹配参数(数组)','变量规则(数组)'],
        ...
    ],'','请求类型','匹配参数(数组)','变量规则');

2.使用

// 动态批量注册
Route::rule([
        "index"=>"index/index/index",
        "diaoyong"=>"index/index/diaoyong",
        "type/:id"=>"index/index/type"
    ],'','get');
Route::get([
        "index"=>"index/index/index",
        "diaoyong"=>"index/index/diaoyong",
        "type/:id"=>"index/index/type"
    ]);

3.设置路由-配置文件批量注册

return [
    "index"=>"index/index/index",
    "diaoyong"=>"index/index/diaoyong",
    "type/:id"=>"index/index/type"
];

五、变量规则

Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数(数组)’,’变量规则(数组)’);

// ['id'=>'d{1,3}','name'=>'w+']设置路由变量规则,id只能是1-3位数字,name只能是hi字符串
Route::rule("course/:id/:name","index/index/course",'get',[],['id'=>'d{1,3}','name'=>'w+']);

六、路由参数

路由参数是指可以设置一些路由匹配的条件参数,主要用于验证当前的路由规则是否有效,主要包括:

Route::rule('course/:id/:name','index/index/course','get',['method'=>'get','ext'=>'html'],['id'=>'d{1,3}','name'=>'w+']);
// 路由参数method 请求方式必需是get
// 路由参数ext 主要设置路由的后缀

参数  说明
method  请求类型检测,支持多个请求类型
ext URL后缀检测,支持匹配多个后缀
deny_ext    URL禁止后缀检测,支持匹配多个后缀
https   检测是否https请求
domain  域名检测
before_behavior 前置行为(检测)
after_behavior  后置行为(执行)
callback    自定义检测方法
merge_extra_vars    合并额外参数
bind_model  绑定模型(V5.0.1+)
cache   请求缓存(V5.0.1+)
param_depr  路由参数分隔符(V5.0.2+)
ajax    Ajax检测(V5.0.2+)
pjax    Pjax检测(V5.0.2+)

七、资源路由

1.声明

Route::resource(‘blog’,’index/blog’);

也可以在定义资源路由的时候限定执行的方法(标识),例如:

Route::resource('blog','index/blog',['only'=>['index','read','edit','update']]);
Route::resource('blog','index/blog',['except'=>['index','delete']]);

2.会动注册7个路由规则(一定要记忆)

标识 请求类型 生成路由规则 对应操作方法(默认)
index GET blog index
create GET blog/create create
save POST blog save
read GET blog/:id read
edit GET blog/:id/edit edit
update PUT blog/:id update
delete DELETE blog/:id delete

八、快捷路由

1.声明

// 声明快捷路由
Route::controller('blog','index/blog');

2.控制器

class Blog
{
    public function geta(){
        echo 'aaaaaaaaa';
    }
}

3.url访问

https://www.tp.com/blog/a (寻找geta方法)
https://www.tp.com/blog/index (寻找getindex方法)

九、url生成

1.系统类

dump(Url::build('index/index/index'));

2.系统方法

dump(url('index/index/index'));

3.使用

public function index()
    {
        echo '我是blog控制器index方法';
        dump(Url::build('index/index/index'));
        dump(url('index/index/index'));

        dump(Url::build('index/index/test'));
        dump(url('index/index/test'));

        dump(Url::build('index/index/course/id/10'));
        dump(url('index/index/course/id/10'));
        //
        dump(Url::build('index/index/abc',['id'=>10,'name'=>'张三']));
        dump(url('index/index/abc',['id'=>10,'name'=>'张三']));
        dump(url('index/index/abc','id=10&name=100'));

        //带锚点
        dump(url('index/blog/read#name','id=5'));
        dump(url('index/blog/read#name',['id'=>5,'name'=>'100']));
        // 带域名
        dump(Url::build('index/blog/read#anchor@blog','id=5'));
        dump(url('index/blog/read#anchor@blog',['id'=>5,'name'=>'100']));
        http://blog.tp.com/blog/read/id/5/name/100.html#anchor

        // 加上入口文件
        Url::root('/index.php');
        dump(url('index/blog/read#anchor@blog',['id'=>5,'name'=>'100']));
        //http://blog.tp.com/index.php/blog/read/id/5/name/100.html#anchor

    }

到此这篇关于thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成的文章就介绍到这了,更多相关thinkphp原理:入口文件、路由模式、路由设置和url生成内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/php/15960.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部