03. Laravel 4 过滤器

基础过滤器

定义过滤器:

// app/filters.php
Route::filter('birthday', function()
{
    if (date('d/m') == '12/12')
    {
        return View::make('birthday');
    }
});

路由中的调用方法:

// app/routes.php
Route::get('/', array(
    'before' => 'birthday',
    function()
    {
        return View::make('hello');
    }
));

多重过滤

采用“|”分隔,或数组定义

// app/routes.php
Route::get('/', array(
    'before' => 'birthday|christmas',
    function()
    {
        return View::make('hello');
    }
));

// app/routes.php
Route::get('/', array(
    'before' => array('birthday', 'christmas'),
    function()
    {
        return View::make('hello');
    }
));

过滤器参数

// app/filters.php
// before 前置过滤器
Route::filter('test', function($route, $request)
{
});
// after 后置过滤器
Route::filter('test', function($route, $request, $response)
{
});
向过滤器中传递额外的参数
// app/routes.php
Route::get('/', array(
    'before' => 'birthday:foo,bar,baz',
    function()
    {
        return View::make('hello');
    }
));

// app/filters.php
Route::filter('birthday', function($route, $request, $first, $second, $third)
{
    return "{$first} - {$second} - {$third}";
});

使用类文件来作为过滤器

首先确保已经 注册 laravel 的类加载目录

// app/start/global.php
ClassLoader::addDirectories(array(
    ...
    app_path().'/filters',  //  如需使用过滤器类,请添加
    ...
));

接着定义过滤器类文件

// app/filters/Birthday.php
class BirthdayFilter {
    ...
    public function filter($route, $request, $date)
    {
        if (date('d/m') == $date)
        {
            return View::make('birthday');
        }
    }
    ...
}

// app/routes.php
Route::filter('birthday', 'BirthdayFilter');

注意: 最终执行的是类中的 filter() 方法,并且新定义的类需要执行一次 php artisan dump-autoload 指令以帮助其载入。

全局过滤器

// app/filters.php
App::before(function($request)
{
    //
});
App::after(function($request, $response)
{
    //
});

模式过滤器

// app/routes.php
Route::when('profile/*', 'birthday');

以上代码将匹配所有 profile/ 开头的路由,来使用 birthday 作为前置过滤器。
还可以针对 HTTP 请求类型 限定模式过滤器:

Route::when('admin/*', 'admin', array('post'));

转载于:https://my.oschina.net/5say/blog/186297

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值