或
return [ 'index'=>'sam/test/index', 'demo/:name/[:class]/'=>['sam/test/demo',['method'=>'GET','ext'=>'html'],['class'=>'\w{1,10}','name'=>'\w{3,8}']], ];-----------
路由参数规则route.php (1)分开写 think\Route::pattern([ 'name'=>'[a-zA-Z]+', 'age'=>'\d{2}' ]); think\Route::get('demo/:name/:age','sam/test/demo'); (2)合并写 return [ '__pattern__'=>[ 'name'=>'[a-zA-Z]+', 'age'=>'\d{2}' ], 'demo/:name/:age'=>'sam/test/demo' ];依赖注入,向类中的方法传递对象的方法 class Temp { private $name; public function __construct($name='Sam') { $this->name=$name; } public function setName($name) { $this->name=$name; } public function getName() { return '方法是:'.__METHOD__.'属性是:'.$this->name; } } ---------- public function getMethod(\app\common\Temp $testtemp) { // 方法里的 \app\common\Temp $testtemp等价于下面这行 // $testtemp = new \app\common\Temp; $testtemp->setName('SamC'); return $testtemp->getName(); } //绑定一个类到容器 public function bindClass() { //把一个类放到容器中(注册到容器) \think\Container::set('temp','\app\common\Temp'); //使用助手函数bind() //bind('temp','\app\common\Temp'); //将容器里的类实例化并取出来 $temp = \think\Container::get('temp',['name'=>'Samphp']); //或 //$temp = app('temp',['name'=>'Samphp']); return $temp->getName(); } //绑定一个闭包到容器(理解为匿名函数) public function bingClosure() { \think\Container::set('demo',function($domain){ return '微语录的网址是:'.$domain; }); //将容器里的闭包取出来用 return \think\Container::get('demo',['domain'=>'www.top789.cn']); }
转载于:https://www.cnblogs.com/samphp/p/8576074.html
相关资源:ThinkPHP中类的构造函数_construct()与_initialize()的区别详解