班等沟腺芯井卷扔烈独氨爬此
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($name,$lession) { return "我是".$name."我在学习".$lession; } }
路由规则:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::rule('demo/:name/:lession','index/index/demo', 'GET',['ext'=>'shtml'],['name'=>'\w{3,8}','lession'=>'\w{1,10}']); //路由解释:需要传入两个参数,请求方式为get,后缀为shtml,name参数是字符长度为3-8 //lession 参数为字符 长度为1-10
执行:
参数:name长度需要在3-8,超出即不符合路由规则,报错:
设置lession默认值为php
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($name,$lession="php") { return "我是".$name."我在学习".$lession; } }
需要在路由规则中设置lession参数为可选参数
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::rule('demo/:name/[:lession]','index/index/demo', 'GET',['ext'=>'shtml'],['name'=>'\w{3,8}','lession'=>'\w{1,10}']); //路由解释:需要传入两个参数[lession参数为可选参数,可以不传参], //请求方式为get,后缀为shtml,name参数是字符长度为3-8 //lession 参数为字符 长度为1-10
执行:
当传入lession参数时,会将lession的默认参数php替换
枢椿讽铝配蹄吭幢淬铂居叙唉