Laravel是否有Rails的before_filters将变量绑定到控制器方法中?

时间:2013-07-03 21:35:04

标签: laravel laravel-4 laravel-3

我是Ruby on Rails的新手。就在几个小时前开始学习Ruby和Ruby on Rails并尝试将它的DRY原则应用到我自己的Laravel代码中。

这就是我的RoR的样子:

class WeddingController < ApplicationController

    before_filter :get_wedding

    def get_wedding
        /*
           If Wedding.find(2) returns false, redirect homepage
           Else, bind @wedding into all methods for use
        */
    end

    def edit
        @wedding //this method has access to @wedding, which contains Wedding.find(2) data.
    end

    def update
        //Same as above method
    end

    def destroy
        //Same as above method, can do things like @wedding.destroy
    end
end

这就是我的Laravel的样子

class Wedding_Controller extends Base_Controller {

public function edit($id)
{
    if(Wedding::find($id) === false)
       return Redirect::to('/);

    //Edit code
}

public function update($id)
{
    if(Wedding::find($id) === false)
       return Redirect::to('/);

    //Update code
}

public function destroy($id)
{
    if(Wedding::find($id) === false)
       return Redirect::to('/);

    //Destroy code
}
}
  1. 如何干if(Wedding::find($id) === false)检查我的RoR代码?
  2. 如果Wedding::find($id) returns actual Wedding data,如何在所有指定方法中将其作为$wedding variable注入? (如果可能的话,不要使用类范围寻找任何东西。)
  3. 非常感谢!

    聚苯乙烯。对于不了解我的RoR脚本的人;基本上就是这样。

    Before any method call on this controller, execute get_wedding method first. 
    If get_wedding can't find the wedding in database, let it redirect to homepage.
    If get_wedding finds the wedding in database, inject returned value as @wedding variable to all methods so they can make use of it. (e.g destroy method calling @wedding.destroy() directly.)
    

2 个答案:

答案 0 :(得分:2)

是的,在过滤器可以操纵传递给路线的数据之前。例如:

Route::filter('wedding', function($route, $request) {
    $id = $route->getParameter('id');
    $wedding = Wedding::findOrFail($id); // if no wedding is found, it returns a 404 here
    // Here is where we hit a small road block. You can call
    $route->setParameters(array($wedding));
    // But you just erased any other parameters the route was accepting.
    // So then you start getting *all* the parameters
    $params = $route->getParameters();
    // Then you try to merge your data in somehow, then you set it back, etc.
});

有一种更简单的方法!

从控制器的结构来看,我假设它是一个资源控制器。请看以下示例:

Route::model('wedding, 'Wedding'); // varname, model name
Route::resource('wedding', 'WeddingController'); // The first param here matches the first param above.

class WeddingController extends BaseController {
    ...
    public function show(Wedding $wedding) {
        return View::make('wedding')->with('wedding', $wedding);
    }
    ...
}

这称为路径模型绑定。有关详细信息,请参阅http://laravel.com/docs/routing#route-model-binding

修改

让我扩展一下这个例子,比如有一天你需要有/wedding/cahill-manley而不是/wedding/123的网址,你可以删除Route::model行,并在其中添加:

Route::bind('wedding', function($value) {
    return Wedding::where('slug', $value)->firstOrFail();
});

然后事情继续发挥作用。

答案 1 :(得分:0)

您可以创建婚礼过滤器并将其应用于婚礼控制器:

public function __construct()
{
    $this->beforeFilter('wedding');
}

我不知道如何复制@ wedding.destroy,但是如果你想在整个地方注入一个变量,你可以使用位于 app的View::share()中的App:before() /filters.php

App::before(function($request)
{

    $check_wedding = Wedding::where('...');
    if($check_wedding)
    {
        View::share('wedding', $check_wedding);
    }
    else
    {
        return Redirect::to('/');
    }

});