ThinkPHP5 相关知识重点笔记
生活随笔
收集整理的這篇文章主要介紹了
ThinkPHP5 相关知识重点笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、相關(guān)配置 1、配置文件:config/config.php <?php return [ // 是否開啟路由 'url_route_on' ? ? ? ? ? => true, ]; ?> 2、獲取配置項(xiàng) <?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獲取,推薦使用這種 } } //設(shè)置配置 Config::set('abc',123); Config::set($arr) 二、路由設(shè)置 config/route.php <?php // think\Route::rule('demo/:class','sam/test/demo','GET',['ext'=>'html'],['class'=>'\w{1,10}']); //單個(gè)設(shè)置 //數(shù)組方式設(shè)置(推薦) return [ 'index'=>'sam/test/index', 'demo/:class'=>['sam/test/demo',['method'=>'GET','ext'=>'html'],'class'=>'\w{1,10}'], ]; ?> -----默認(rèn)值(接上面)------------- http://tp5.cn/demo/yan/javascript.html public function demo($name,$class='php') { return '這是'.$name.'老師的正在學(xué)習(xí)'.$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}']], ];-----------
路由參數(shù)規(guī)則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' ];?
依賴注入,向類中的方法傳遞對(duì)象的方法 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等價(jià)于下面這行 // $testtemp = new \app\common\Temp; $testtemp->setName('SamC'); return $testtemp->getName(); } //綁定一個(gè)類到容器 public function bindClass() { //把一個(gè)類放到容器中(注冊(cè)到容器) \think\Container::set('temp','\app\common\Temp'); //使用助手函數(shù)bind() //bind('temp','\app\common\Temp'); //將容器里的類實(shí)例化并取出來 $temp = \think\Container::get('temp',['name'=>'Samphp']); //或? //$temp = app('temp',['name'=>'Samphp']); return $temp->getName(); } //綁定一個(gè)閉包到容器(理解為匿名函數(shù)) public function bingClosure() { \think\Container::set('demo',function($domain){ return '微語錄的網(wǎng)址是:'.$domain; }); //將容器里的閉包取出來用 return \think\Container::get('demo',['domain'=>'www.top789.cn']); }轉(zhuǎn)載于:https://www.cnblogs.com/samphp/p/8576074.html
總結(jié)
以上是生活随笔為你收集整理的ThinkPHP5 相关知识重点笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python解析url、提取url参数、
- 下一篇: poj2109 Power of