laravel 在RouteServiceProvidor中找不到Laravel 5路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29155143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:10:13  来源:igfitidea点击:

Laravel 5 Route not Found in RouteServiceProvidor

phplaravelnamespaces

提问by Weasler

PHP Fatal error:  Class 'App\Providers\Route' not found in /home/****/**********/app/Providers/RouteServiceProvider.php on line 28



  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Class 'App\Providers\Route' not found

Above is the error of which I am seeing whenever attempting to run Artisan. It seems that the issue has to do with my Filters (I am attempting to migrate from Laravel 4 to 5), and Namespacing. I have two namespace's for which I want all of these filters to apply to: "App\Http\Controllers\API\V1" and "App\Http\Controllers\API\V2", but cannot for the life of me understand how to set this in the RouteServiceProvidor file. Below is the code from RSP.php up to the relevant line.

以上是我在尝试运行 Artisan 时看到的错误。这个问题似乎与我的过滤器(我试图从 Laravel 4 迁移到 5)和命名空间有关。我有两个命名空间,我希望所有这些过滤器都适用于:“App\Http\Controllers\API\V1”和“App\Http\Controllers\API\V2”,但我一生都无法理解如何在 RouteServiceProvidor 文件中设置它。下面是从 RSP.php 到相关行的代码。

<?php namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider {

    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        parent::boot($router);

        //Challenges
        Route::filter('challenge_general_permission', function($route)

Any ideas?

有任何想法吗?

回答by Alan Storm

Your PHP file is in the App\Providersnamespace.

您的 PHP 文件位于App\Providers命名空间中。

namespace App\Providers;

You attempted to use a global class Routewith a relative class name reference.

您试图使用Route具有相对类名引用的全局类。

Route::filter('challenge_general_permission', function($route)

When you do this, PHP assumes you want the class App\Providers\Route. There's no such class -- PHP dies. You either need to tell PHP you want the global class Route

执行此操作时,PHP 假定您需要 class App\Providers\Route。没有这样的类——PHP 死了。你要么需要告诉 PHP 你想要全局类Route

\Route::filter('challenge_general_permission', function($route)

or import Routeinto the current namespace

或导入Route到当前命名空间

namespace App\Providers;
//...
use Route;

回答by lukasgeiter

@AlanStorms answer is totally correct but I wanted to add that you don't even have to use the Routefacade. Especially in this case where you have the $routerobject directly available. By the way: Routeris the underlying class of the Routefacade. I'd do this instead of what you have now:

@AlanStorms 的答案是完全正确的,但我想补充一点,您甚至不必使用Route外观。特别是在这种情况下,您可以$router直接使用对象。顺便说一句:RouterRoute门面的底层类。我会这样做而不是你现在拥有的:

public function boot(Router $router)
{
    parent::boot($router);

    //Challenges
    $router->filter('challenge_general_permission', function($route)