用于本應用的控制器自動加載類設置,用法如同\CodeIgniter\Config\AutoloadConfig
自動加載識別文件:dayrui/App/應用目錄/Config/Auto.php
語法格式:<?php
//?自動加載識別文件
return?[
/**
*?命名空間映射關系
*/
'psr4'?=>?[
],
/**
*?類名映射關系
*/
'classmap'?=>?[
],
];
實現自動化引用類文件,可以對系統類路徑進行重新,非常方便的開發方式
一、命名空間映射關系示例
1、創建自動加載識別文件:dayrui/App/Myapp/Config/Auto.php<?php
//?自動加載識別文件
return?[
/**
*?命名空間映射關系
*/
'psr4'?=>?[
'MyXunrui'?=>?APPPATH.'Xunrui/',
//?前面的MyXunrui是命名空間的名稱
//?后面的是儲存目錄,專門儲存用到?的類文件
],
];
2、創建其中一個測試類文件? dayrui/App/Myapp/Xunrui/Test.php<?php
namespace?MyXunrui;
class?Test?{
function?test()?{
return?'MyXunrui?test';
}
}
類文件首字母大寫.php,
類方法名稱必須和文件名保存一致,首字母大寫,
必須聲明namespace和(1)中的名稱保持一致
3、創建控制器,來調用這個類
dayrui/App/Myapp/Controllers/Test.php<?php ?namespace?Phpcmf\Controllers;
class?Test?extends?\Phpcmf\Common
{
public?function?index()?{
$obj?=?new?\MyXunrui\Test();
echo?$obj->test();
}
}
此方法可以創建多個類文件,只需要按(2)中的命名來建立
4、訪問
/index.php?s=myapp&c=test&m=index
就能輸出 MYxunrui test
二、類名映射關系示例
1、創建自動加載識別文件:dayrui/App/Myapp/Config/Auto.php<?php
//?自動加載識別文件
return?[
/**
*?類名映射關系
*/
'classmap'?=>?[
'MyXunruiClass'?=>?APPPATH.'xunrui.php',
],
];
2、創建這個文件APPPATH.'xunrui.php' 表示?dayrui/App/Myapp/xunrui.php<?php
class?MyXunruiClass?{
function?test()?{
return?'test!';
}
}
3、創建控制器,來調用這個類
dayrui/App/Myapp/Controllers/Test.php<?php ?namespace?Phpcmf\Controllers;
class?Test?extends?\Phpcmf\Common?{
public?function?index()?{
$obj?=?new?\MyXunruiClass();
echo?$obj->test();?//?這樣就執行上面定義的類,不需要手動去加載他
}
}
多個類需要在(1)中做多次映射
4、訪問
/index.php?s=myapp&c=test&m=index
就能輸出 test!
三、第三方php類組件映射關系示例
1、將下載的源碼復制到插件Myapp目錄,例如:
dayrui/App/Myapp/src/
2、創建自動加載識別文件:dayrui/App/Myapp/Config/Auto.php<?php
//?自動加載識別文件
return?[
/**
*?命名空間映射關系
*/
'psr4'?=>?[
'QL\QueryList'?????????????=>?APPPATH.'src',
],
];
3、創建控制器,來調用這個類
dayrui/App/Myapp/Controllers/Test.php<?php ?namespace?Phpcmf\Controllers;
use?QL\QueryList;
class?Test?extends?\Phpcmf\Common
{
public?function?index()?{
$data?=?QueryList::get('https://www.xunruicms.com')->find('img')->attrs('src');
//打印結果
print_r($data->all());
}
}
4、訪問
/index.php?s=myapp&c=test&m=index
就能輸出 結果
本文地址:https://www.xunruicms.com/doc/767.html