4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证

篱持姓忱魂柒千穆门画香熊缉

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第1张

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第2张

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第3张

复制惯例配置文件中的URL参数方式:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第4张

D:\phpStudy\PHPTutorial\WWW\tp5\config\config.php:

<?php
return [
    // 是否开启路由
    'url_route_on'=> true,
    // 是否强制使用路由
    'url_route_must'=> false,
    // URL参数方式 0 按名称成对解析 1 按顺序解析
    'url_param_type'         => 1,
];

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        return '111';
    }
    public function test($name,$age)
    {
        return "我的姓名是:".$name.",我的年龄是".$age;
    }
}

D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:

<?php
think\Route::get('test/:name/:age','index/index/test');

执行:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第5张


变量规则验证

D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php

<?php
think\Route::get('test/:name/:age','index/index/test',[],[
    'name'=>'[a-zA-Z]+',//name必须是a-z,A-Z之间的字符,+:至少有一个字符
    'age'=>'\d{2}'//age必须是整数,且必须是两位数
]);
//因为变量规则必须是第4个参数,所以第三个参数路由参数使用一个空数组代替

执行:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第6张

数字大于2位数:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第7张

不是纯英文字符串

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第8张

说明验证规则生效


4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第9张

D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php

<?php

think\Route::pattern([
    'name'=>'[a-zA-Z]+',//name必须是a-z,A-Z之间的字符,+:至少有一个字符
    'age'=>'\d{2}'//age必须是整数,且必须是两位数
]);

think\Route::get('test/:name/:age','index/index/test');

执行:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第6张

数字大于2位数:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第7张

不是纯英文字符串

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第8张

说明规则生效


简化,将全局的变量规则和路由规则写到一起,使用路由配置数组实现

<?php
return [
    '__pattern__'=>[
        'name'=>'[a-zA-Z]+',//name必须是a-z,A-Z之间的字符,+:至少有一个字符
        'age'=>'\d{2}'//age必须是整数,且必须是两位数
    ],
    'test/:name/:age'=>'index/index/test'
];

执行:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第6张

数字大于2位数:

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第7张

不是纯英文字符串

4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第8张

说明规则生效


4-7ThinkPHP5路由规则中变量规则的设置技巧:全局/局部/正则验证第16张

惫狗渡椭理详陕既蹿沤氏癌窍