首页 > 解决方案 > Laravel 路由不适用于子文件夹

问题描述

使用从 4.2 迁移的 Laravel 5 现在 laravel 5 安装在子文件夹“abc”中,我必须为每条路线编写 abc/warehouse 吗?以前是 /warehouse。我想在子目录 abc 中使用所有现有路由,例如 /warehouse。

我在 localhost xampp 上,端口为 81。 http://localhost:81/warehouse

这里有快速解决方案的任何人

标签: laravellaravel-5routeslaravel-5.1

解决方案


You use prefix when defining routes:

Route::prefix('abc')->group(...)

Route Prefixes

Route::prefix('abc')->group(function () {
    Route::get('warehouse', function () { 
        // Matches The "/abc/warehouse" URL
     }); 
});

Ideally you should do it in the RouteServiceProvider

Route::middleware('web')
   ->prefix('abc')
   ->namespace($this->namespace)
   ->group(base_path('routes/web.php'));

This way everything in the routes file is prefixed and you dont need the extra group wrapping.

Here's the example from the 5.0 docs:

Route::group(['prefix' => 'admin'], function() {
    Route::get('users', function() { 
        // Matches The "/admin/users" URL
     });
 });

推荐阅读