Home > PHP Framework > Laravel > body text

About routing adjustments in laravel8

藏色散人
Release: 2022-01-31 04:00:38
forward
2532 people have browsed it

下面由Laravel教程栏目给大家介绍laravel8更新之路由调整,希望对大家有所帮助!

在Laravel的早期版本中,RouteServiceProvider包含一个$namespace属性。该属性的值将自动添加到控制器路由定义和对actionhelper /方法的调用之前。

About routing adjustments in laravel8

在Laravel 8.x中,默认情况下为此属性。这意味着Laravel不会自动命名空间前缀。因此,在新的Laravel 8.x应用程序中,应使用标准的PHP可调用语法定义控制器路由定义:URL::actionnull

About routing adjustments in laravel8

被注释掉了。

所以在laravel8中加载路由需要加上命名空间,如下。

use App\Http\Controllers\Admin\AdminController;

Route::get('/admin', [AdminController::class, 'index']);
Copy after login

或者也可以

use App\Http\Controllers\Admin;
# 注:这里第二个参数是数组
Route::get('/admin', [Admin\AdminController::class, 'index']);
Copy after login

如果是资源路由,则要:

# 注意这里第二个参数是类,字符串,不要传数组
 Route::resource('/admin', Admin\AdminController::class);
Copy after login

对action相关方法的调用应使用相同的可调用语法:

action([Admin\AdminController::class, 'index']);

return Redirect::action([Admin\AdminController::class, 'index']);
Copy after login

注意

如果您更喜欢Laravel 7.x样式控制器的路由前缀,则可以简单地将$namespace属性添加到应用程序的中RouteServiceProvider。
路径为:

app/Providers/RouteServiceProvider.php
Copy after login

找到该文件,将被注释的一行代码取消注释即可使用之前版本的自动载入命名空间的写法。29行

// protected $namespace = 'App\\Http\\Controllers';
Copy after login

推荐学习:《laravel视频教程

The above is the detailed content of About routing adjustments in laravel8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!