使用前缀时路由 URI 通配符

Routing URI wildcard when using prefix

我一直在为我的根路径 (/) 使用路由通配符,直到我决定将我的前端文件移到前缀 (/crm) 后面。在那之后我得到 404 并且不知道如何解决它。我需要通配符作为我的 Javascript 前端路由 (/crm/orders/details/12345) 的全部捕获,否则会导致 404.

因此,当我删除 prefix('crm') 时,使用以下代码设置的所有内容都可以正常工作。或者当我删除 {any} 加上匹配的正则表达式时,它在一层深度路由上部分起作用(/crm 起作用,但是 /crm/orders 没有)。

但是当我同时拥有前缀和通配符时 /crm 给我一个 404。

我需要如何配置?

有效

Providers/RouteServiceProvider.php:

Route::namespace('App\Http\Controllers')
  ->group(base_path('routes/crm.php'));

routes/crm.php

Route::get('/{any}', function ()
{
    return view('crm');
})->where('any', '.*');

不起作用

Providers/RouteServiceProvider.php:

Route::prefix('crm')
  ->namespace('App\Http\Controllers')
  ->group(base_path('routes/crm.php'));

routes/crm.php

Route::get('/{any}', function ()
{
    return view('crm');
})->where('any', '.*');

phpartisanroute:list的输出如下

| Domain | Method | URI |Name | Action | Middleware  |
*snap*
| | GET|HEAD | crm/{any} | | Closure | |
*snap*

您需要将 {any} 配置为可选参数。在您的特定情况下,该路线只会捕获 crm/something 路线(其中某些东西可以属于一个或多个路段)。

Route::prefix('crm')->group(function () {
    Route::get('/{any?}', function () {
        dd("I am here");
    })->where('any', '.*');
});

但是,如果您将 {any} 添加为可选,它也会捕获 /crm。这是你想要的?