柏剖盗俱凤妮凹镐投筋牌诗慧
ThinkPHP5.0.8核心版下载:
http://www.thinkphp.cn/donate/download/id/968.html
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() { return "萌面人资料铺"; } }
路由规则:
<?php think\Route::rule('demo','index/index/demo','GET',[],[]); //第一个[]:路由参数,第二个[]:变量规则
执行:
直接使用路由的get方法
<?php //直接使用get方法 think\Route::get('demo','index/index/demo',[],[]);
执行:
使用配置文件方式定义
路由定义:D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get'],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 ];
执行:
设置扩展名D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','ext'=>'shtml|html'],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 ];
执行:只允许扩展名为shtml或者html,没有扩展名会报错
扩展名为空:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','ext'=>''],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 ];
执行,有扩展名报错,没有扩展名成功(需要版本为5.0.7以上才行)
没有扩展名,执行成功(thinkPHP版本必须5.0.7及以上)
不允许扩展名为空:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','deny_ext'=>''],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 ];
执行,没有扩展名报错,输入默认的扩展名.html成功:
1.没有拓展名报错
2.输入默认的扩展名成功
默认的扩展名可以在:D:\phpStudy\PHPTutorial\WWW\tp5\thinkphp\convention.php
惯例配置文件中查看:
设置配置文件内容:
<?php return [ 'site_domain'=>'www.xuexianswer.com' ];
在公共文件common.php中创建公共函数my_check()
路由配置:
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','callback'=>'my_check'],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 //'callback'=>'my_check':当函数my_check返回true时,才会跳转到默认控制器下的demo方法 ];
执行:
注释配置文件中的site_domain
<?php return [ // 'site_domain'=>'www.xuexianswer.com' ];
执行:
只允许域名为tp5.com
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','domain'=>'tp5.com'],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 //'callback'=>'my_check':当函数my_check返回true时,才会跳转到默认控制器下的demo方法 ];
执行:
其他域名:
<?php return [ 'demo'=>['index/index/demo',['method'=>'get','domain'=>'tp35.com'],[]] //代码解释:参数意义:路由地址 请求方式 []:变量规则 //'callback'=>'my_check':当函数my_check返回true时,才会跳转到默认控制器下的demo方法 ];
执行:
嫂劝刻僵裳汇菇扒廖魂堤诉咕