Reputation: 1635
What is/are the difference between the following route on laravel?
Route::resource('posts', 'Admin\PostsController');
Route::resource('posts', Admin\PostsController::class);
when should i use and which one should i use?
Thanks.
Upvotes: 1
Views: 192
Reputation: 50491
They are completely different things.
'PostsController' is the string 'PostsController'
Admin\PostsController::class
is most likely the string 'App\Http\Controllers\Admin\PostsController'
If you use the second one it will end up looking for App\Http\Controllers\App\Http\Controllers\Admin\PostsController
as Laravel is already assuming the namespace for the controllers to be App\Http\Controllers
based on what is set in your RouteServiceProvider
.
When you use just 'PostsController' Laravel ends up with App\Http\Controllers\PostsController
.
This is how its setup by default. (assuming you have no groups that are adjusting the namespace used)
Upvotes: 1
Reputation: 4012
The difference between the two is that the first is going to look for a Controller within the same scope as the file in which you are specifying your routes. The second will look for a controller called PostsController
iin the Admin
namespace.
If these locations are one and the same, there will be no difference between the two routes.
In answer to the second part of the question, which one you choose to use is really up to you. I personally make use of route groups to handle my namespacing (among other things) like so:
Route::group([
'namespace' => 'Admin',
], function () {
Route::resource('posts', 'PostsController');
}
Upvotes: 0