1. 程式人生 > >ThinkPHP5 相關知識重點筆記

ThinkPHP5 相關知識重點筆記

conf abc nbsp 文件 true top dom gpo 註冊

一、相關配置 1、配置文件:config/config.php <?php return [ // 是否開啟路由 ‘url_route_on‘ => true, ]; ?> 2、獲取配置項 <?php namespace app\index\controller; use think\Controller; use think\Config; class Index { public function index() { //return config(‘site_name‘);配置方法獲取 return dump(Config::get(‘database‘));//get獲取,推薦使用這種 } } //設置配置 Config::set(‘abc‘,123); Config::set($arr) 二、路由設置
config/route.php <?php // think\Route::rule(‘demo/:class‘,‘sam/test/demo‘,‘GET‘,[‘ext‘=>‘html‘],[‘class‘=>‘\w{1,10}‘]); //單個設置 //數組方式設置(推薦) return [ ‘index‘=>‘sam/test/index‘, ‘demo/:class‘=>[‘sam/test/demo‘,[‘method‘=>‘GET‘,‘ext‘=>‘html‘],‘class‘=>‘\w{1,10}‘], ]; ?> -----默認值(接上面)------------- http://tp5.cn/demo/yan/javascript.html public function demo($name,$class=‘php‘) { return ‘這是‘.$name.‘老師的正在學習‘.$class; } think\Route::rule(‘demo/:name/[:class]/‘,‘sam/test/demo‘,‘GET‘,[‘ext‘=>‘html‘],[‘class‘=>‘\w{1,10}‘,‘name‘=>‘\w{3,8}‘]);

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‘]); }

ThinkPHP5 相關知識重點筆記