【问题标题】:Beginning Laravel 4 - Keeping the controller skinny开始 Laravel 4 - 保持控制器瘦身
【发布时间】:2014-01-29 20:07:28
【问题描述】:

我正在尝试维护一个纤细的控制器,但我仍然习惯于控制器中可以放入的东西,因为之前我习惯将所有东西都堆放在里面。在此示例中,我将经过验证的数据插入到数据库中,我认为这是正确的。我感到困惑的是,我想获取一个字段输入,操作其文本格式,然后将其保存到我数据库中的另一个字段。我写的作品,但我不知道在我的控制器中有这段代码是否好,如果不是应该去哪里?

控制器

public function store()
{
    $validation = new Services\Validators\Deal;

    if($validation->passes())
    {
        $deals = Deals::create(Input::all());

        // start code in question
        $image = Input::get('company');
        $image = strtolower($image);
        $image = str_replace(" ", "-", $image);
        $image .= ".png";

        $deals->image = $image;
        $deals->save();
        // end code in question

        return Redirect::to('deals/create')
            ->with('message', 'Deal Created');
    }

    return Redirect::back()
        ->withInput()
        ->withErrors($validation->errors);
}

回顾一下,我不确定有问题的代码是否属于我的控制器,如果不属于,放在哪里会更好?感谢您提供任何见解。

【问题讨论】:

    标签: php laravel laravel-4 separation-of-concerns


    【解决方案1】:

    任何业务逻辑都应该放在模型或存储库中,并且您的控制器应该看起来像

    <?php
    
    class DealsController extends controller {
    
        public function __construct(Deals $deals) //// <---- Dependency Injection
        {
            $this->deals = $deals;
        }
    
        public function store()
        {
            try 
            {
                $this->deals->insertRow(Input::all());
            } 
            catch (\Exceptions\ValidationException $e) 
            {
                return Redirect::back()
                    ->withInput()
                    ->withErrors($this->deals->errors);
            }
    
            return Redirect::to('deals/create')
                ->with('message', 'Deal Created');
        }
    
    }
    

    在您的 Deals 类中,您可以对数据做任何您需要做的事情

    class Deals extends Eloquent {
    
        public function insertRow($input)
        {
            $validation = new Services\Validators\Deal;
    
            if($validation->passes())
            {
                $deals = Deals::create($input);
    
                // start code in question
                $image = $input['company'];
                $image = strtolower($image);
                $image = str_replace(" ", "-", $image);
                $image .= ".png";
    
                $deals->image = $image;
                $deals->save();
                // end code in question
            }
    
            $this->errors = $validation->errors;
    
            throw new \Exceptions\ValidationException("Error inserting Deals", $validation->errors);
        }
    
    }
    

    这是未经测试且未真正重构的代码,但我希望您能从中看到一点。

    【讨论】:

    • 谢谢!这很好地回答了我的问题。
    【解决方案2】:

    您实际上可以删除所有有问题的代码并改用 Laravel Mutator。

    基本上,在您的 Deals 类中设置一个函数,该函数将在即将通过 Model Eloquent ::create 或更新设置/保存数据时自动处理文本格式。

    有点像

    public function setImageAttribute($value)
    {
        $image = strtolower($value);
        $image = str_replace(" ", "-", $image);
        $image .= ".png";
    
        $this->attributes['image'] = $image;
    }
    

    参考http://laravel.com/docs/eloquent#accessors-and-mutators

    【讨论】:

    • 感谢您的提示,我不知道 Laravel Mutator
    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2013-07-04
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多