Laravel 中的方法过滤器不存在错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34973173/
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 13:05:44  来源:igfitidea点击:

Method filter does not exist error in laravel

phplaravelroutes

提问by AL-zami

i am new to laravel and trying to play with different aspects of this framework.Currently i am working with route::filters and wrote a simple code to see how filters works.But i am getting the following error:

我是 laravel 的新手,并尝试使用该框架的不同方面。目前我正在使用 route::filters 并编写了一个简单的代码来查看过滤器的工作原理。但我收到以下错误:

BadMethodCallException in Macroable.php line 81: Method filter does not exist.

Macroable.php 第 81 行中的 BadMethodCallException:方法过滤器不存在。

why is this error thrown ?how i can solve this issue? Here is my code in route.php :

为什么会抛出这个错误?我该如何解决这个问题?这是我在 route.php 中的代码:

Route::filter('birthday', function()
{
    if (true) {
        return View::make('birthday');
    }
});
Route::get('/', array(
    'before' => 'birthday',
    function()
    {
       return View::make('welcome');
    }
));

回答by Imtiaz Pabel

route filters are not going away entirely after Laravel 5.0However, middleware is now the preferred way to achieve the same functionality. See http://laravel.com/docs/master/middlewarefor info about how to use it. Middleware can be implemented to behave like either "before" or "after" filters. And it can be applied to all routes (called "global middleware"), or assigned to specific routes (by adding "'middleware' => 'auth'" for example to your route definitions in your routes.php file.

在 Laravel 5.0 之后路由过滤器并没有完全消失但是,中间件现在是实现相同功能的首选方式。有关如何使用它的信息,请参阅http://laravel.com/docs/master/middleware。中间件可以实现为“之前”或“之后”过滤器的行为。它可以应用于所有路由(称为“全局中间件”),或分配给特定路由(例如,通过将“'中间件' => 'auth'”添加到 routes.php 文件中的路由定义。