在Laravel 5.0中的RouteServiceProvider中使用\ App :: before()

时间:2015-09-26 13:21:18

标签: php laravel laravel-4 laravel-5 laravel-routing

从Laravel 4.2迁移到Laravel 5.0。我将\App::before(function($request)从我的L4.2 filters.php移到了L5.0的RouteServiceProvider.php的boot()方法,但我得到了一个

FatalErrorException in Facade.php line 210: Call to undefined method Illuminate\Foundation\Application::before()

错误抛出它。我做错了什么?

我没有找到任何说这不应该起作用的东西; an SO question/answer says that it worked for another guy,所以我不确定我的代码在哪里不同。我尝试过包含use Illuminate\Support\Facades\App;\App::before,但无济于事。

<?php namespace App\Providers;

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

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\App;

class RouteServiceProvider extends ServiceProvider {

    protected $namespace = 'App\Http\Controllers';

    public function boot(Router $router)
    {

        App::before(function($request)
        {
            //My before code
        });
    }

1 个答案:

答案 0 :(得分:4)

试试Middleware。在您的情况下,您可以使用artisan命令创建新的中间件。使用以下命令获取帮助:

php artisan help make:middleware

然后在App \ Http \ Kernel.php中使用register中间件

最后使用Route Group MiddlewareController Middleware进行保护。