帕错娃沦标吞抢瓦比槽乃途坍
将路由改为混合模式:
创建路由规则:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::rule('demo/:lession','index/index/demo', 'GET',['ext'=>'shtml'],['lession'=>'\w{1,10}']); //路由解释:demo规则需要传递一个参数lession,请求类型为get,后缀是shtml //参数lession必须符合正则规则,即必须是字符,且长度在1-10
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use think\Config; class Index { public function index() { return "www.xuexianswer.com"; } public function demo($lession) { return "我在学习".$lession; } }
执行,此时直接执行http://tp5.com/demo会报错,因为没有传入参数
直接传参也不行,因为后缀不是shtml
正确调用:必须传参,且后缀是shtml,且参数为字符,长度为1-10
当有变量lession时,叫做动态配置,没有变量时为静态配置。
使用配置数组来进行配置
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::rule('demo/:lession','index/index/demo', // 'GET',['ext'=>'shtml'],['lession'=>'\w{1,10}']); //将上方的代码使用配置数组来改写 return [ 'demo/:lession'=>['index/index/demo',['method'=>'get','ext'=>'shtml'],['lession'=>'\w{1,10}']] ];
执行:
如果路由规则简单,则不需要使用数组
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
创建test方法:
<?php namespace app\index\controller; use think\Config; class Index { public function index() { return "www.xuexianswer.com"; } public function demo($lession) { return "我在学习".$lession; } public function test() { return "我是index模块下的index控制器下的test方法"; } }
路由规则:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::rule('demo/:lession','index/index/demo', // 'GET',['ext'=>'shtml'],['lession'=>'\w{1,10}']); //将上方的代码使用配置数组来改写 return [ 'demo/:lession'=>['index/index/demo',['method'=>'get','ext'=>'shtml'],['lession'=>'\w{1,10}']], 'test'=>'index/index/test' ];
执行:
绊价情芳肥瓦特段秽箔深臼纲