ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 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类中。