What is the correct order to inject dependencies into Laravel controllers?

Asked

Viewed 43 times

1

I have noticed that sometimes, depending on the order I inject the dependencies, they don’t work...

Is there any correct order?

1 answer

1


In the builder, can be solved any dependency and does not have an order, but when the resolution is done by the methods the order is relative to the route parameters, which will always be after all dependencies that need to be solved, example:

Route:

Route::post('save/{id}', 'AdminController@save');
Route::post('view/{id}/{slug}', 'NoticeController@view');

Method:

public function save(Request $request, $id)
{
}

public function view(Request $request, RepositoryNotice $repository, $id, $slug)
{
}

then, the route vestments are always placed at the end of the method, explanation is in the documentation

References Dependency Injection & Controllers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.