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

1

I have noticed that sometimes, depending on the order that I inject the dependencies, they do not work ...

Is there a correct order?

    
asked by anonymous 09.02.2018 / 01:49

1 answer

1

In constructor , any dependency can be resolved and does not have an order, but when the resolution is done by methods the order is relative to the route parameters, which will always be after all the dependencies that need to be resolved, 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 params are always placed at the end of the method, explanation is in the documentation

References Dependency Injection & Controllers

    
09.02.2018 / 21:11