Laravel - How do I inject automatically in Validat

2019-07-23 05:53发布

I'm not able to inject these variables through Laravel:

//...class AllowedUsername implements Rule...

public function __construct(Router $router, Filesystem $files, Repository $config)
{
    $this->router = $router;
    $this->files = $files;
    $this->config = $config;
}

I get the error:

 Type error: Too few arguments to function ... 0 passed in.

Why is Laravel not doing it automatically?

$request->validate([
            'username' => ['required', new AllowedUsername],           
        ]);

1条回答
老娘就宠你
2楼-- · 2019-07-23 06:16

In order to leverage Laravel's injection magic you need to use Laravel's API which essentially is:

  • resolve($class) which is wrapper around app($class)
  • app($class, $params = []) which is wrapper around:

Note: I've changed $abstract for $class

if (is_null($class)) {
    return Container::getInstance();
}

return Container::getInstance()->make($class, $parameters);

Classes that you want to resolve out of container (as seen in your code sample):

public function __construct(Router $router, Filesystem $files, Repository $config)

can be resolved only because Laravel maintainers already defined binding for Router::class, Filesystem:class (example: FilesystemServiceProvider).

Repository::class seems to be simple class that does not require parameters (or require parameters that container already knows how to resolve) while "newing up" - thus Laravel can resolve it without problem.

There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.

Thats why resolve(AllowedUser::class) or resolve(Router::class)... work.


In order to let Laravel know what constructor's parameters should be sent during "newing up" you use bindings mentioned in documentation.

查看更多
登录 后发表回答