码迷,mamicode.com
首页 > Web开发 > 详细

ThinkPHP5 相关知识重点笔记

时间:2018-03-15 21:03:40      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:容器   mon   注册   sam   方式   对象   重点   names   log   

一、相关配置
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 相关知识重点笔记

标签:容器   mon   注册   sam   方式   对象   重点   names   log   

原文地址:https://www.cnblogs.com/samphp/p/8576074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!