donpvtzuux37724 2018-12-14 21:38
浏览 60

使用:: class在config中传递class,并在Laravel中检索

I have a config file with such array:

'ppr' => [
    'validate' => TestRequest::class
];

Now, I want to retrive this class in other part of the system and use it to validate form (outside of the controller).

While using config('main.ppr.validate') all I receive is namespaced name of the class instead of the class object.

As I already accepted that it won't be that easy to just use reuqests as in controllers, I still do wonder how to pass a class by config. While passing eloquent models it works like a charm (or i.e. config arrays with middlewares etc.), so I suppose there is some magic binding to the IoC to achive that, is it true?

My question is, how to use class passed as in example above without initializing it like:

$obj = new $className;
  • 写回答

1条回答 默认 最新

  • douguxun6866 2018-12-14 21:53
    关注

    Laravel (and many other applications) use Dependency Injection to achieve this magic -- your words, not mine! :D

    It seems that the Service Container is what handles this in Laravel and should be of help to you.

    Directly from the Laravel docs (linked above):

    Within a service provider, you always have access to the container via the $this->app property. We can register a binding using the bind method, passing the class or interface name that we wish to register along with a Closure that returns an instance of the class:

    $this->app->bind('HelpSpot\API', function ($app) {
        return new HelpSpot\API($app->make('HttpClient'));
    });
    

    Also:

    You may use the make method to resolve a class instance out of the container. The make method accepts the name of the class or interface you wish to resolve:

    $api = $this->app->make('HelpSpot\API');
    

    And:

    If some of your class' dependencies are not resolvable via the container, you may inject them by passing them as an associative array into the makeWith method:

    $api = $this->app->makeWith('HelpSpot\API', ['id' => 1]);
    

    IMHO, I would look up where/how this is implemented in the native Laravel code (usually the Illuminate vendor) and see how it is used / meant to be implemented.


    Furthermore, ClassName::class will return the namespace + class of that class. This is why you only see the class name and are not actually receiving an object/instance of that class.

    I'm not sure what/where/why/how you're implementing your class and why you need this functionality somewhere that it doesn't already exist. Laravel is pretty good about already having things set up where you need them, so think twice before breaking out of the box and make sure there isn't a default solution for your situation!

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大