场双镣闻妨线暴彻斯侩矾路熄
打开惯例配置文件:D:\phpStudy\PHPTutorial\WWW\tp5\thinkphp\convention.php
将访问控制器层:controller
改为:api
将index模块下的controller改为api
将Index控制器的命名空间改为api
<?php namespace app\index\api; use think\Config; class Index { public function index() { return "www.xuexianswer.com"; } public function demo($name,$lession="php") { return "我是".$name."我在学习".$lession; } }
执行:
如果Index控制器没有更改,仍然是controller
<?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; } }
则会提示如下错误:
将访问控制器层改回controller
执行:
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; } }
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::get('demo/:name','index/index/demo?lession=thinkphp5'); //代码解释:将可选参数lession作为额外参数写到demo方法中:?lession=thinkphp5
执行:
路由规则:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::get('demo/:name','index/index/demo?lession=thinkphp5'); //代码解释:将可选参数lession作为额外参数写到demo方法中:?lession=thinkphp5
创建index模块的配置文件config.php
<?php return [ 'site_domain'=>'www.xuexianswer.com' ];
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") { $domain=\think\Config::get('site_domain'); return "我是".$name."我在学习".$lession.'网址:'.$domain; } }
执行:
给路由规则添加@
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ think\Route::get('demo/:name','@index/index/demo?lession=thinkphp5'); //代码解释:将可选参数lession作为额外参数写到demo方法中:?lession=thinkphp5
执行:
可以看到网址消失了,这是因为使用了@跳过了模块的初始化,直接执行方法,所以针对index模块进行的配置也就失效了。
在应用目录下创建Test类
<?php namespace app; class Test { public function test() { return "我是自定义类Test的test()方法"; } }
路由规则:
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::get('demo/:name','@index/index/demo?lession=thinkphp5'); think\Route::get('test','\app\Test@test');
执行:
路由规则:
D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::get('demo/:name','@index/index/demo?lession=thinkphp5'); //think\Route::get('test','\app\Test@test'); think\Route::get('myfunc',function(){ return "我是跳转的闭包函数的内容"; });
执行:
该操作可以用于网站更新的内容提示:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::get('demo/:name','@index/index/demo?lession=thinkphp5'); //think\Route::get('test','\app\Test@test'); think\Route::get('myfunc',function(){ return "网站更新中。。。"; });
执行:
闭包函数接收参数
路由规则:
<?php //需要使用think下的Route类,因为该类全局有效,所以不需要加\ //think\Route::get('demo/:name','@index/index/demo?lession=thinkphp5'); //think\Route::get('test','\app\Test@test'); think\Route::get('myfunc/:lession',function($lession){ return "我在学习".$lession; });
执行
在public下创建demo.php
<?php echo "<h1>欢迎来到萌面人资料铺,我是mengmianren</h1>";
路由规则:D:\phpStudy\PHPTutorial\WWW\tp5\config\route.php:
<?php think\Route::rule('myjump','/demo.php');
执行:
站外跳转:
路由规则:
<?php think\Route::rule('myjump','http://www.baidu.com');
执行:http://tp5.com/myjump 将跳转到百度
猾浅蜜邵灰跨釜都锰贬秀姓京