Composer autoload ¶
Slim3 Request Response ¶
Composer autoload ¶
有四種
- PSR-4
- PSR-0
- classmap
- files
PSR-4 ¶
- Under the
psr-4 keyyou define a mapping from namespaces to paths, relative to the package root. - Namespace prefixes must end in \ to avoid conflicts between similar prefixes.
- 可以在
vendor/composer/autoload_psr4.php找到 key=>value array
{
"autoload": {
"psr-4": {
"": "classes/",
"Foo\\": "bar/",
"Foo\\Bar\\": "bar/",
"Foo\\": "bar2/bar1/",
"Monolog\\": ["src/", "lib/"] # multi dir
}
}
}
對應路徑 ¶
The prifix Foo will not present in the file path
src/classes
src/bar/
src/Bar/bar
src/bar2/bar1/
classmap ¶
- 讀取 dir 底下所有檔案
{
"autoload": {
"classmap": [
"controllers",
"models"
]
}
}
讀取 controllers 和 models 底下所有檔案
file ¶
{
"files": [
"path/to/file.php"
]
}
PSR-0 ¶
過時了,不講
Slim3 Container Resolution ¶
問題 ¶
class GameController{
protected $ci;
...
public function __construct($ci)
{
$this->ci = $ci
}
...
}
當初這段怎麼看都不懂,搞不懂 $ci 到底從哪裡來的
原因 ¶
Alternatively, if the class does not have an entry in the container, then Slim will pass the container’s instance to the constructor.
slim 會自動引入到 construct 裡
PS ¶
$this->ci->get('logger')和$this->ci->logger是一樣的意思- 使用
composer dump-autoload更新 autoload
Slim3 Request Response ¶
問題 ¶
class GameController{
...
public function openGame($request, $response, array $args)
{
...
$params = $request->getParams();
...
}
...
}
找不到任何地方有引入 use Slim\Http\Response; use Slim\Http\Request; 之類的東西,覺得很奇怪為啥能執行
原因 ¶
不知道,反正就是能執行,只要不要寫成
public function openGame(Request $request, Response $response, array $args)
就不會報錯,而且不管有沒有加上 Request 兩者型態還是一樣
還有 $request->getParams(); 這個 function 不知道從哪裡蹦出來的,就記下來吧
不過建議用
$request->getQueryParams()吧
覺得可能是 PSR-7 的原因,但是不太確定