简体   繁体   English

在Laravel 5.2中的控制器内使用服务提供者

[英]Use of service providers within controllers in Laravel 5.2

As for the title I've goggled about two hours searching for a efficient answer and read repeatedly the official documentation, but without any step further, considering I'm relatively new to the framework. 至于标题,我花了大约两个小时的时间来寻找有效的答案,并反复阅读了官方文档,但考虑到我对框架相对较新,则没有任何进一步的改进。 The doubt arise while searching for a correct way to share some code between controllers and i stumbled in service providers, so: 在寻找一种在控制器之间共享一些代码的正确方法时,我产生了疑问,我偶然发现了服务提供商,因此:

  1. I've created say a MyCustomServiceProvider ; 我创建了一个MyCustomServiceProvider
  2. I've added it to the providers and aliases arrays within the app.php file; 我已经将它添加到app.php文件中的providersaliases数组app.php
  3. finally I've created a custom helpers class and registered it like: 最后,我创建了一个自定义帮助程序类,并将其注册为:

     class MyCustomServiceProvider extends ServiceProvider { public function boot() { // } public function register() { $this->app->bind('App\\Helpers\\Commander', function(){ return new Commander(); }); } } 

So far, however, if I use that custom class within a controller I necessarily need to add the path to it through the use statement: 但是,到目前为止,如果我在控制器中使用该自定义类,则必须通过use语句为其添加路径:

use App\Helpers\Commander;

otherwise I get a nice class not found exception and obviously my controller does not his job. 否则,我得到一个很好的类,未发现异常,显然我的控制器没有执行他的工作。
I suspect there's something which escapes to me on service providers! 我怀疑在服务提供商中有些事情对我不利! :-) :-)

So far, however, if I use that custom class within a controller I necessarily need to add the path to it through the use statement: 但是,到目前为止,如果我在控制器中使用该自定义类,则必须通过use语句为其添加路径:

 `use App\\Helpers\\Commander;` 

otherwise I get a nice class not found exception and obviously my controller does not his job. 否则,我得到一个很好的类,未发现异常,显然我的控制器没有执行他的工作。

Yes, that's how it works. 是的,就是这样。 If you don't want to use the full name, you can use a Facade instead. 如果您不想使用全名,则可以改用Facade

Create the Facade class like this: 像这样创建Facade类:

class Commander extends Facade
{
    protected static function getFacadeAccessor() { return 'commander'; }
}

register the service: 注册服务:

$this->app->singleton('commander', function ($app) {
    return new Commander();
});

add the alias to your config/app.php : 将别名添加到您的config/app.php

'aliases' => [
    //...
    'Commander' => Path\To\Facades\Commander::class,
    //...
],

and use it like a Facade : 并像Facade一样使用它:

\Commander::doStuff();

On why your code still works, even when you remove the bind: 关于为什么即使删除绑定也仍然可以使用代码的原因:

When you type-hint a parameter to a function, and Laravel does not know about the type you want (through binding), Laravel will do its best to create that class for you, if it is possible. 当您向函数键入参数提示时,Laravel不知道您想要的类型(通过绑定),Laravel会尽力为您创建该类。 So even though you didn't bind the class, Laravel will happily create a instance of that class for you. 因此,即使您没有绑定该类,Laravel也会为您愉快地创建该类的实例。 Where you actually need the binding is when you use interfaces . 您真正需要绑定的地方是使用接口时 Usually, you'd not type-hint specific classes but a interface. 通常,您不是类型提示特定的类,而是接口。 But Laravel can not create a instance of an interface and pass it to you, so Laravel needs to know how it can construct a class which implements the interface you need. 但是Laravel 无法创建接口的实例并将其传递给您,因此Laravel需要知道它如何构造一个实现所需接口的类。 In this case, you'd bind the class (or the closure which creates the class) to the interface. 在这种情况下,您可以将类(或创建类的闭包)绑定到接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM