【问题标题】:How can I set a catch all route as the very last route in Laravel我如何将一条全部路线设置为 Laravel 中的最后一条路线
【发布时间】:2016-07-11 10:37:48
【问题描述】:

我正在构建一个带有 SEO slug 的基本 CMS,所以我想在我的路线尽头抓住一切来获得 slug。所以在我的路线文件末尾我添加了:

Route::get('/{page?}', ['as' => 'Page', 'middleware' => 'web', 'uses' => 'PageController@index']);

这很好,直到我添加了Laravel File Manager,它有自己的路线。这些路由是在我的主路由文件中的所有路由之后添加的。因此,现在我的全部内容都为文件管理器提供了任何内容。

如何在运行 catch all 之前加载所有其他路由,包括其他供应商文件夹中的路由?有没有办法可以声明该路由不能与任何前缀为laravel-filemanager 的路由匹配?我无法在 Laravel 文档中或通过 Google 找到任何关于此的内容。

这里要求的是我的应用提供商:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
    Caffeinated\Modules\ModulesServiceProvider::class,
    Intervention\Image\ImageServiceProvider::class,
    Spatie\Activitylog\ActivitylogServiceProvider::class,
    Spatie\Permission\PermissionServiceProvider::class,
    Unisharp\Ckeditor\ServiceProvider::class,
    Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,

],

【问题讨论】:

  • 您能列出应用程序提供商吗?一种解决方案可能是在路由服务提供者之前列出文件管理器服务提供者
  • @GeorgeGarchagudashvili 我已经为你添加了我的应用程序提供程序,文件管理器是最后一个。

标签: php laravel-5.2 laravel-routing


【解决方案1】:

只需尝试重新排序 app.php 中的服务提供程序,如下所示:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */

    Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,
    ... 
    App\Providers\RouteServiceProvider::class,
    ... 

],

无论如何,如果不是特殊情况,我总是在列表末尾写RouteServiceProvider

【讨论】:

  • 谢谢,没有意识到提供者的顺序影响了路由加载的顺序。
  • 欢迎您,很高兴为您提供帮助。 laravel 很大,所以在遇到这种情况之前,您无法实现一切;))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2016-04-22
  • 1970-01-01
  • 2021-08-20
相关资源
最近更新 更多