php Laravel - 类型错误:参数太少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43958616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Type error: Too few arguments?
提问by I'll-Be-Back
I get an error:
我收到一个错误:
Type error: Too few arguments
类型错误:参数太少
I thought Laravel do some magic to if arguments is not fully passed?
如果参数没有完全通过,我认为 Laravel 会做一些魔术吗?
For example:
例如:
In the Controller I have:
在控制器中,我有:
public function create(CreateRequest $request)
{
return $this->todoService->createList($request);
}
In the todoService
class:
在todoService
课堂上:
use App\Plan
class todoService {
public function createList($request, Plan $plan)
{
//
}
}
As you can see I did not pass Plan
class object. Do I have to bind or something?
如您所见,我没有传递Plan
类对象。我必须绑定还是什么?
采纳答案by Hafiz
If you are calling createList() by yourself so you will need to pass both parameters by yourself. You can bind something with Plan but still if you will call something not Laravel, then you will be responsible to pass that function parameters.
如果您自己调用 createList() ,则需要自己传递这两个参数。你可以用 Plan 绑定一些东西,但如果你调用的不是 Laravel 的东西,那么你将负责传递那个函数参数。
This type hinting only works if Laravel is calling that function. So you need to call these functions through Laravel.
这种类型提示仅在 Laravel 调用该函数时有效。所以需要通过 Laravel 来调用这些函数。
If you are trying to automatically injecting in a class's constructor, you can simply do this:
如果您尝试自动注入类的构造函数,您可以简单地执行以下操作:
$service = $this->app->make('App\Plan\todoservice');
or
或者
$service = App::make('App\Plan\todoservice');
or
或者
$service = resolve('App\Plan\todoservice');
But this will only work for Constructors. Please note that you can also provide parameters as next arguments of make()
or resolve()
function.
但这仅适用于构造函数。请注意,您还可以提供参数作为make()
或resolve()
函数的下一个参数。
In fact, different methods can also be called that way.
事实上,不同的方法也可以这样调用。
You can simply do:
你可以简单地做:
$service = App::make('App\Plan\todoservice');
$container->call([$service, 'createList'], ['request' => $request] );
Here $container
is object of Illuminate\Contracts\Container\Container
.
这$container
是 的对象Illuminate\Contracts\Container\Container
。
回答by wujt
You have to bind
classes only if they depend on interfaces. If you specify particular class, reflection will do the job for you. documentation
bind
只有当它们依赖于接口时,你才需要类。如果您指定特定的类,反射将为您完成这项工作。文件
The only way this will work, is to set the default value of second parameter. In any other situation, syntax exception will be thrown.
这将起作用的唯一方法是设置第二个参数的默认值。在任何其他情况下,都会抛出语法异常。
use App\Plan
class todoService
{
public function createList($request, Plan $plan = null)
{
//
}
}
public function create(CreateRequest $request)
{
return $this->todoService->createList($request);
}
It will work, but will that make any sense?
它会起作用,但这有意义吗?
回答by Niels Keurentjes
Laravel cannot do any magic on this level as your coding error is simply a PHP syntax error. You're indicating that the second parameter is of type Plan
, which makes it mandatory implicitly. Laravel cannot 'patch' simple function calls like this.
Laravel 不能在这个级别上做任何魔术,因为您的编码错误只是一个 PHP 语法错误。您表示第二个参数是 type Plan
,这使得它隐式成为强制性的。Laravel 不能像这样“修补”简单的函数调用。
Your confusion is likely in that, depending on your routing, Laravel can inject the correct Plan
parameter into the create
controller method, thus allowing you to forward it into the service.
您的困惑可能在于,根据您的路由,Laravel 可以将正确的Plan
参数注入create
控制器方法,从而允许您将其转发到服务中。
回答by edfgdfgdfg
/**
* Create a new personal access token for the user.
*
* @param string $name
* @param array $scopes
* @return \Laravel\Passport\PersonalAccessTokenResult
*/
public function createToken($name, array $scopes = [])
{
return Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
$this->getKey(), $name, $scopes
);
}
/**
* Set the current access token for the user.
*
* @param \Laravel\Passport\Token $accessToken
* @return $this