How to load routes in laravel8
Most people don't understand the knowledge points of this article "How to load routes in laravel8", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to load routes in laravel8".
RouteServiceProvider In earlier versions of Laravel, RouteServiceProvider included a $namespace attribute. The value of this attribute is automatically added to the controller route definition and before calls to the actionhelper /method.
In Laravel 8.x, this property is by default. This means Laravel does not automatically prefix namespaces. Therefore, in new Laravel 8.x applications, controller routing definitions should be defined using standard PHP callable syntax: URL::actionnull
was commented out.
So loading routes in laravel8 requires adding namespaces, as follows.
use App\Http\Controllers\Admin\AdminController;Route::get('/admin', [AdminController::class, 'index']);
or may
use App\Http\Controllers\Admin;#Note: The second argument here is the array Route::get ('/admin',[Admin\AdminController::class, 'index']);
If this is a resource route, you would:
#Note that the second parameter here is class, string, do not pass array Route::resource ('/admin', Admin\AdminController::class);
Calls to action-related methods should use the same callable syntax:
action([Admin\AdminController::class, 'index']);return Redirect::action([Admin\AdminController::class, 'index']); Note
If you prefer the routing prefix for Laravel 7.x style controllers, you can simply add the $namespace attribute to RouteServiceProvider in your application.
The path is:
app/Providers/RouteServiceProvider.php
Find the file, uncomment the commented line of code and use the previous version's auto-load namespace. 29 lines
// protected $namespace = 'App\\Http\\Controllers'; The above is about the content of this article on "How to load routes in laravel8". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.