原來在thinkphp5下的Model.php實(shí)現(xiàn)了JSON 序列化接口, 調(diào)用了Model.php的toArray方法,所以json_encode才會(huì)輸出這樣的數(shù)據(jù)出來.
實(shí)現(xiàn) JsonSerializable 的類可以在 json_encode() 時(shí)定制他們的 JSON 表示
// JsonSerializable
public function jsonSerialize()
{
return $this->toArray();
}
參考資料:
JSON 序列化接口
$types = empty($types)? array('jpg', 'gif', 'png', 'jpeg'):$types;
$img = str_replace(array('_','-'), array('/','+'), $request->input('image'));
$b64img = substr($img, 0,100);
if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $b64img, $matches)){
$type = $matches[2];
if(!in_array($type, $types)){
return array('type'=>'0','msg'=>'圖片格式不正確','url'=>'');
}
$img = str_replace($matches[1], '', $img);
$img = base64_decode($img);
$photo = 'upload/links/'.md5(date('YmdHis').rand(1000, 9999)).'.'.$type;
file_put_contents(env('IMAGE_URL').'/'.$photo, $img);
$thumbnail=self::resizeImage('0.5',$photo,'thumbnail');
$min =self::resizeImage('0.2',$photo,'min');
return ['type'=>'1','url'=>'/'.$photo,'thumbnail'=>$thumbnail,'min'=>$min];
}
$config = [
//others
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
'gii' => [
'class' => 'yii\gii\Module',
];
],
//others
]
class yii\base\Application extends yii\base\Module
class yii\base\Module extends yii\di\ServiceLocator
class yii\di\ServiceLocator extends yii\base\Component
class yii\base\Component extends yii\base\Object
public function __construct($config = [])
{
Yii::$app = $this;
//將\yii\base\Application中的所有的屬性和方法交給Yii::$app->loadedModules數(shù)組中
static::setInstance($this);
$this->state = self::STATE_BEGIN;
//加載配置文件的框架信息 如:設(shè)置別名,設(shè)置框架路徑等等最為重要的是給加載默認(rèn)組件
$this->preInit($config);
//加載配置文件中的異常組件
$this->registerErrorHandler($config);
// 調(diào)用父類的 __construct。
// 由于Component類并沒有__construct函數(shù)
// 這里實(shí)際調(diào)用的是 `yii\base\Object__construct($config)`
Component::__construct($config);
}
Component::__construct($config)
會(huì)調(diào)用 yii\base\Object::__construct()
方法public function __construct($config = [])
{
if (!empty($config)) {
// 將配置文件里面的所有配置信息賦值給Object。
// 由于Object是大部分類的基類,
// 實(shí)際上也就是有配置信息賦值給了yii\web\Application的對(duì)象
Yii::configure($this, $config);
}
$this->init();
}
'components' => [ 'log' => [...]]
從哪來,若不關(guān)心可以直接看 第二步。$this->preInit($config);
,即 yii\base\Application::preInit(&$config)
public function preInit(&$config)
{
//others...
// merge core components with custom components
// 合并核心組件和自定義組件
foreach ($this->coreComponents() as $id => $component) {
if (!isset($config['components'][$id])) {
// 若自定義組件中沒有設(shè)置該核心組件配置信息,直接使用核心組件默認(rèn)配置
$config['components'][$id] = $component;
} elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
// 若自定義組件有設(shè)置該核心組件配置信息,但是沒有設(shè)置 'class'屬性,則添加該class屬性
$config['components'][$id]['class'] = $component['class'];
}
}
}
/**
* Returns the configuration of core application components.
* 返回核心應(yīng)用組件的配置
* @see set()
*/
public function coreComponents()
{
return [
// 日志分配器組件
'log' => ['class' => 'yii\log\Dispatcher'],
//others...
];
}
$this->preInit($config);
, 我們得到的 $config
$config = [
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
'gii' => [
'class' => 'yii\gii\Module',
];
],
'components' => [
'log' => [
'class' => 'yii\\log\\Dispatcher',
],
//others...
]
//others...
]
'components' => [ 'log' => [...]]
從哪來yii\base\Object::__construct($config = [])
中的 Yii::configure($this, $config);
public static function configure($object, $properties)
{
// 只是遍歷配置信息,賦值給當(dāng)前對(duì)象
foreach ($properties as $name => $value) {
$object->$name = $value;
}
return $object;
}
yii\base\Object::__set($name, $value)
/**
* 為實(shí)例不存在的屬性賦值時(shí)調(diào)用
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$object->property = $value;`.
* 這個(gè)是PHP的魔術(shù)方法,會(huì)在執(zhí)行 `$object->property = $value;` 的時(shí)候自動(dòng)調(diào)用。
*/
public function __set($name, $value)
{
// setter函數(shù)的函數(shù)名
// 由于php中方法名稱不區(qū)分大小寫,所以setproperty() 等價(jià)于 setProperty()
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// 調(diào)用setter函數(shù)
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
// 如果只有g(shù)etter沒有setter 則為只讀屬性
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
當(dāng)前情景下的 $object
我們可以認(rèn)為是 yii\base\Application
的對(duì)象 $app
$app->modules = [
'debug' => [
'class' => 'yii\debug\Module',
],
'gii' => [
'class' => 'yii\gii\Module',
];
]
這里會(huì)調(diào)用 yii\base\Module::setModules($modules)
方法
public function setModules($modules)
{
foreach ($modules as $id => $module) {
$this->_modules[$id] = $module;
}
}
這樣便有了問題中的
["_modules":"yii\base\Module":private]=>
array(3) {
["debug"]=> object(yii\debug\Module)#33 (28) {
......
}
["gii"]=> object(yii\gii\Module)#113 (21) {
......
}
...
}
$app->components = [
'log' => [
'class' => 'yii\\log\\Dispatcher',
],
]
yii\di\ServiceLocator::setComponents($components)
方法public function setComponents($components)
{
foreach ($components as $id => $component) {
$this->set($id, $component);
}
}
public function set($id, $definition)
{
// others ...
if (is_object($definition) || is_callable($definition, true)) {
// an object, a class name, or a PHP callable
$this->_definitions[$id] = $definition;
} elseif (is_array($definition)) {
// 定義如果是個(gè)數(shù)組,要確保數(shù)組中具有 class 元素
// a configuration array
if (isset($definition['class'])) {
// 定義的過程,只是寫入了 $_definitions 數(shù)組
$this->_definitions[$id] = $definition;
} else {
throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
}
} else {
throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
}
}
這樣便有了問題中的
["_definitions":"yii\di\ServiceLocator":private]=>
array(24) {
["errorHandler"]=> .....
["request"]=> ......
["log"]=> ......
......
}
env('DB_USERNAME', 'forge')
代碼是在 .env 文件里沒有配置 DB_USERNAME
時(shí)才采用第二個(gè)參數(shù)的值。
你已經(jīng)通過 .env 文件里的數(shù)據(jù)配置項(xiàng)去配置數(shù)據(jù)庫(kù)信息,所以再 database.php 去修改配置是沒有作用的。
根據(jù)錯(cuò)誤提示:你是使用 root 賬號(hào)去訪問 localhost 的本地 mysql,但是你的 .env 和 database.php 文件中都是配置的一個(gè)具體 IP 地址。
所以肯定不是去讀取的這兩個(gè)文件,所以需要確定:
由此可以猜測(cè)應(yīng)該是瀏覽器的某個(gè)機(jī)制的問題導(dǎo)致了這個(gè)問題
解決方法:可以試一下完全退出瀏覽器程序后再重新打開訪問
樓主CURL構(gòu)建沒有問題,那么建議以下檢查一下
Request
的Content-Type
,這里是有需求設(shè)置為application/json
嗎?其實(shí)不重要哈Authorization
的前綴約束是否一致,可多做嘗試或深讀文檔json對(duì)象是類數(shù)組的結(jié)構(gòu), 可以用sizeof函數(shù)判斷長(zhǎng)度
$data1=json_decode('{"MCVersion":"v1.10.0","JavaVersion":"v1.8","Server":"Linux"}', true);
var_dump($data1);
echo sizeof($data1);
將會(huì)輸出
array(3) {
["MCVersion"]=>
string(7) "v1.10.0"
["JavaVersion"]=>
string(4) "v1.8"
["Server"]=>
string(5) "Linux"
}
3
注意json_decode第二個(gè)參數(shù)要用true, 將返回array類型,可以用sizeof, 否則將返回stdClass.
where的用法很多多條件
數(shù)組條件:
$map['name'] = 'thinkphp';
$map['status'] = 1;
// 把查詢條件傳入查詢方法
Db::table('think_user')->where($map)->select();
字符串條件
Db::table('think_user')->where('type=1 AND status=1')->select();
具體可查看tp5官方手冊(cè)
代碼:
$arr = [
['value' => '顏色', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['白色','黑色']],
['value' => '包裝', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['大','小']],
['value' => '規(guī)則', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['1','2']]
];
function decare($arr){
$data = [];
$res = [];
for ($i=0; $i < count($arr)-1; $i++) {
if($i == 0){
$data = $arr[$i]['detail'];
}
//替代變量1
$rep1 = [];
foreach ($data as $v) {
foreach ($arr[$i+1]['detail'] as $g) {
//替代變量2
$rep2 = ($i!=0?'':$arr[$i]['value']."_").$v."-".$arr[$i+1]['value']."_".$g;
$tmp[] = $rep2;
if($i==count($arr)-2){
foreach (explode('-', $rep2) as $k => $h) {
//替代變量3
$rep3 = explode('_', $h);
//替代變量4
$rep4['detail'][$rep3[0]] = $rep3[1];
}
$res[] = $rep4;
}
}
}
$data = $tmp;
}
return [$data,$res];
}
print_r(decare($arr)[1]);
結(jié)果:
mysql查詢是在磁盤上面進(jìn)行IO操作,php循環(huán)是在內(nèi)存中進(jìn)行,你的第一個(gè)方法的循環(huán)查詢會(huì)慢一些。 如果數(shù)據(jù)庫(kù)中數(shù)據(jù)量過大,內(nèi)存占用過大,建議分片讀取處理
request()->user();
or
auth('api')->user();
沒辦法從 token 里面直接拿到 id,必須先通過這種方式獲取當(dāng)前 token 的 User 對(duì)象。
使用消息隊(duì)列的方式。登錄成功后,往隊(duì)列中丟一個(gè)登錄成功的消息。
活動(dòng)上線期間,對(duì)隊(duì)列中的登錄消息進(jìn)行消費(fèi);
活動(dòng)下線后,可以不消費(fèi)隊(duì)列中的登錄消息。
$數(shù)組3 = array_merge($數(shù)組1, $數(shù)組2);
怎么說呢 現(xiàn)在比較常用的還是通過.env文件設(shè)置本地環(huán)境的相關(guān)配置吧
不將.env文件加入版本控制, 導(dǎo)出的時(shí)候就不存在.env文件就不用擔(dān)心替換的事情.
使用setStatusCode方法就好
response()->json()->setStatusCode(200)
php內(nèi)置數(shù)組: array_column()
訪問量高嗎?
如果訪問量不高,可以開通流量收費(fèi),設(shè)一個(gè)峰值,用多少流量,付多少錢
你的想法以前也有人想過的, 然后 pjax 就出來了
如果是Java的話,Robot 類有這個(gè)功能
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。