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],
]);
In order to leverage Laravel's injection magic you need to use Laravel's API which essentially is:
resolve($class)
which is wrapper aroundapp($class)
app($class, $params = [])
which is wrapper around:Classes that you want to resolve out of container (as seen in your code sample):
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.Thats why
resolve(AllowedUser::class)
orresolve(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.