如何在Laravel 4.1的filter.php中创建DRY

时间:2015-03-25 10:00:21

标签: php laravel-4

我正在使用Laravel4.1。

我希望在不同的过滤器和相同的Object中进行相同的处理,如下所示。

Route::filter('filter1', function(){
  $sameObject = // I want to fetch sameObject;
  View::share('obj', $sameObject);
});

Route::filter('filter2', function(){
  $sameObject = // I want to fetch sameObject;
  if(isset($sameObject){
    return Redirect::to('home')
  }
});

有没有办法让DRY过程?

1 个答案:

答案 0 :(得分:1)

尝试使用session或config(gimmick)方法:

Route::filter('filter1', function(){
  $sameObject = // I want to fetch sameObject;
  Config::set('myObject',$sameObject);
  View::share('obj', $sameObject);
});

Route::filter('filter2', function(){
  $sameObject = Config::get('myObject');
  if(isset($sameObject){
    return Redirect::to('home')
  }

});