duanjianxiu9400
2019-01-31 13:19
浏览 398
已采纳

使用前缀时路由URI通配符

I have been using a route wildcard for my root path (/) successfully until I decided to move my frontend files behind a prefix (/crm). After that I am getting 404's and have no idea how to solve it. I need the wildcard as a catch all for my Javascript frontend routes (/crm/orders/details/12345) which otherwise result in a 404.

So when I remove prefix('crm') everything as set up with the below code works fine. Or when I remove {any} plus the matching regex, it works partially, on one level deep routes (/crm works, but /crm/orders does not).

But when I have both the prefix and wildcard /crm gives me a 404.

How do I need to configure this?

Works

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', '.*');

Does not work

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', '.*');

Output of php artisan route:list is as follows

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

图片转代码服务由CSDN问答提供 功能建议

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

所以当我删除前缀时( 'crm')使用以下代码设置的所有内容都可以正常工作。 或者,当我删除 {any} 加上匹配的正则表达式时,它会在一个级别的深层路由上工作( / crm 有效,但是 / crm / orders 没有)。

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

我如何配置?

Works

Providers / RouteServiceProvider.php:

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

routes / crm.php

  Route :: get('/ {any}',function()
 {
 n返回视图('crm'); 
  }) - &gt; where('any','。*'); 
   
 
 

不起作用

提供商/ RouteServiceProvider.php:

  Route :: prefix('crm')
  - &gt; namespace('App \ Http \ Controllers')
  - &gt; group(base_path  ( '路由/ crm.php')); 
   
 
 

路由/ crm.php

 <代码>路线::  get('/ {any}',function()
 {
返回视图('crm'); 
}) - &gt; where('any','。*'); 
    
 
 

php artisan rout的输出 e:list 如下

  | 域名| 方法|  URI |名称| 行动| 中间件| 
 * snap * 
 |  |  GET | HEAD |  crm / {any} |  | 关闭|  | 
 * snap * 
   
 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjianlu0506 2019-01-31 13:50
    已采纳

    You need to configure {any} as an optional parameter. In your particular case, the route will only catch crm/something routes (where something can be of one or more segments).

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

    However, if you add the {any} as optional, it will also catch /crm. Is this what you are looking for?

    打赏 评论

相关推荐 更多相似问题