php – Laravel 5 – 方法注入

前端之家收集整理的这篇文章主要介绍了php – Laravel 5 – 方法注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
方法注入如何在Laravel 5(我的意思是实现)中工作,我可以在自定义方法中注入参数,而不仅仅是在控制器操作中吗?
1)阅读本文以了解有关laravel 5中方法注入的更多信息 @H_502_5@http://mattstauffer.co/blog/laravel-5.0-method-injection

@H_502_5@https://laracasts.com/series/whats-new-in-laravel-5/episodes/2

@H_502_5@2)这是方法注入的简单实现

$parameters = [];
$reflector = new ReflectionFunction('myTestFunction');
foreach ($reflector->getParameters() as $key => $parameter) {
    $class = $parameter->getClass();
    if ($class) {
        $parameters[$key] = App::make($class->name);
    } else {
        $parameters[$key] = null;
    }
}
call_user_func_array('myTestFunction',$parameters);
@H_502_5@你也可以看看功能

public function call($callback,array $parameters = [],$defaultMethod = null)
@H_502_5@在https://github.com/laravel/framework/blob/master/src/Illuminate/Container/Container.php
文件了解更多详情

@H_502_5@3)您可以使用方法注入自定义方法

App::call('\App\Http\Controllers\Api\myTestFunction');
@H_502_5@或方法

App::call([$object,'myTestMethod']);
原文链接:https://www.f2er.com/laravel/132538.html

猜你在找的Laravel相关文章