## Container Resolution容器識別解析
You are not limited to defining a function for your routes. In Slim there are a few different ways to define your route action functions.
> 您不受限于為路由定義一個函數。在Slim中有幾種不同的方法來定義您的路由操作函數。
In addition to a function, you may use:
> 除了功能外,你可以使用:
* container\_key:method
* Class:method
* Class implementing`__invoke()`method
* container\_key
This functionality is enabled by Slim’s Callable Resolver Class. It translates a string entry into a function call. Example:
> 此功能由Slim的可調用沖突解決程序類啟用。它將字符串項轉換為函數調用。例子:
~~~php
$app->get('/', '\HomeController:home');
~~~
Alternatively, you can take advantage of PHP’s`::class`operator which works well with IDE lookup systems and produces the same result:
> 或者,您可以利用PHP的`::class`操作符,它與IDE查找系統工作良好,并產生相同的結果:
~~~php
$app->get('/', \HomeController::class . ':home');
~~~
In this code above we are defining a`/`route and telling Slim to execute the`home()`method on the`HomeController`class.
> 在上面的代碼中,我們定義了一個`/`路由,并告訴Slim在`HomeController`類上執行`home()`方法。
>
Slim first looks for an entry of`HomeController`in the container, if it’s found it will use that instance otherwise it will call it’s constructor with the container as the first argument. Once an instance of the class is created it will then call the specified method using whatever Strategy you have defined.
> Slim首先在容器中查找`HomeController`條目,如果找到,它將使用該實例,否則它將調用它的構造函數,并將容器作為第一個參數。一旦創建了類的實例,它將使用您定義的任何策略調用指定的方法。
### Registering a controller with the container向容器注冊控制器
Create a controller with the`home`action method. The constructor should accept the dependencies that are required. For example:
> 使用`home`操作方法創建一個控制器。構造函數應該接受所需的依賴項。例如:
~~~php
<?php
class HomeController
{
protected $view;
public function __construct(\Slim\Views\Twig $view) {
$this->view = $view;
}
public function home($request, $response, $args) {
// your code here
// use $this->view to render the HTML
return $response;
}
}
~~~
Create a factory in the container that instantiates the controller with the dependencies:
> 在容器中創建一個工廠,用依賴關系實例化控制器:
~~~php
$container = $app->getContainer();
$container->set('HomeController', function (ContainerInterface $c) {
$view = $c->get('view'); // retrieve the 'view' from the container從容器中取出“視圖”
return new HomeController($view);
});
~~~
This allows you to leverage the container for dependency injection and so you can inject specific dependencies into the controller.
> 這允許您利用容器進行依賴項注入,這樣您就可以將特定的依賴項注入到控制器中。
### Allow Slim to instantiate the controller允許Slim實例化控制器
Alternatively, if the class does not have an entry in the container, then Slim will pass the container’s instance to the constructor. You can construct controllers with many actions instead of an invokable class which only handles one action.
> 或者,如果類在容器中沒有條目,那么Slim將把容器的實例傳遞給構造函數。您可以構造具有多個操作的控制器,而不是只處理一個操作的invokable類。
~~~php
<?php
use Psr\Container\ContainerInterface;
class HomeController
{
protected $container;
//構造函數接收容器實例
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function home($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
public function contact($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
}
~~~
You can use your controller methods like so.
> 你可以這樣使用你的控制器方法。
~~~php
$app->get('/', \HomeController::class . ':home');
$app->get('/contact', \HomeController::class . ':contact');
~~~
### Using an invokable class使用invokable類
You do not have to specify a method in your route callable and can just set it to be an invokable class such as:
> 你不需要指定一個方法在您的路線可調用,可以只設置它是一個invokable類,如:
~~~php
<?php
use Psr\Container\ContainerInterface;
class HomeAction
{
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function __invoke($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
}
~~~
You can use this class like so.
> 您可以這樣使用這個類。
~~~php
$app->get('/', \HomeAction::class);
~~~
Again, as with controllers, if you register the class name with the container, then you can create a factory and inject just the specific dependencies that you require into your action class.
> 同樣,與控制器一樣,如果您將類名注冊到容器中,那么您可以創建一個工廠并將您需要的特定依賴項注入到action類中。
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir