【问题标题】:Refactoring Laravel 4 App重构 Laravel 4 应用程序
【发布时间】:2014-01-03 12:34:21
【问题描述】:

我正在重构我的 Laravel 4 应用程序,我想要一些关于我的方法和最佳实践的建议。我正在努力做到尽可能 DRY,同时也遵循 SOLID 原则。

我有一个更新方法来更新我的应用程序中的委托。委托和事件之间存在多对多关系,我需要在更新时检查是否满足几个条件才能执行正确的操作/功能。

例如,到目前为止,我检查了代表是否是未分配给事件的浮动代表,如果是,则需要检查事件容量、重考容量和考试容量,同时还要检查是否已选择将委托移动到的事件。

public function update($id)
{
    $delegate = $this->delegate->findById($id);
    $input = Input::all();

    if ($delegate->isFloater())
    {
        if (empty($input['new_event_id']))
        {
            $message = (object) array(
                'title'         => 'Oops!',
                'content'       => 'You did not select an event to move this floating delegate to.',
                'alert_type'    => 'error'
            );

            return Redirect::back()->withInput()->with('message', $message);
        }

        $newEvent = $this->event->findById($input['new_event_id']);

        if ( ! $newEvent->hasCapacity())
        {
            $message = (object) array(
                'title'         => 'Oops!',
                'content'       => 'The event you are trying to move this floating delegate to has no availability.',
                'alert_type'    => 'error'
            );

            return Redirect::back()->withInput()->with('message', $message);
        }

        if ( ! $newEvent->hasResitCapacity($input))
        {
            $message = (object) array(
                'title'         => 'Oops!',
                'content'       => 'The event you are trying to move this floating delegate to has no resit spaces left.',
                'alert_type'    => 'error'
            );

            return Redirect::back()->withInput()->with('message', $message);
        }

        if ( ! $newEvent->hasExamCapacity($input))
        {
            $message = (object) array(
                'title'         => 'Oops!',
                'content'       => 'The event you are trying to move this floating delegate to has no exam spaces left.',
                'alert_type'    => 'error'
            );

            return Redirect::back()->withInput()->with('message', $message);
        }

        $this->delegate->moveFloater($id, $input, $newEvent);

        $message = (object) array(
            'title'         => 'Excellent!',
            'content'       => 'This floating delegate was successfully moved.',
            'alert_type'    => 'success'
        );

        return Redirect::to('admin/events')->with('message', $message);
    }
}

它肯定比我以前的要好得多,我正在将代码抽象为各种模型方法,例如 hasCapacity() 和 isFloater() 等,但该方法已经很大了,我正在重复有关消息的代码和重定向。

目前我不确定如何最好地处理这个问题,我正在寻找一些关于如何继续并进一步清理它的建议。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: refactoring laravel-4 dry solid-principles


    【解决方案1】:

    首先,delegate能否移动是业务/域逻辑,不属于控制器。将与此相关的所有逻辑移动到存储库/服务类中,如果出现问题,该类返回 false,然后使用 getError 方法获取错误消息。

    其次,构造'messages'的flash变量可以抽象成一个受保护的方法实现DRY。

    第三,“更新”来描述一个控制器动作,它显然只是为了将代表从一个事件移动到另一个事件是误导性的,所以我将它重命名为“移动”。

    public function move($id)
    {
        $delegate = $this->delegate->findById($id);
    
        if (!$delegate) {
            return $this->notFound();
            // or something like this...
            App::abort(404);
        }
    
        $input = Input::all();
    
        if (!$this->delegate->move($delegate, $input)) {
            $error = $this->delegate->getError();
            $message = $this->wrapMessage('error', 'Oops!', $error)
            return Redirect::back()->withInput()->with('message', $message);
        }
    
        $message = $this->wrapMessage('success', 'Excellent!', 'This floating delegate was successfully moved.');
        return Redirect::to('admin/events')->with('message', $message);
    }
    
    protected function wrapMessage($type, $title, $content)
    {
        return (object) array(
            'title'      => $title,
            'content'    => $content,
            'alert_type' => $type
        );
    }
    

    【讨论】:

    • 安德烈亚斯,这确实帮助我解决了问题。我真的很感谢你的努力。谢谢老兄。
    • 一个问题,虽然队友,这将如何工作 $error = $this->delegate->getError();错误来自哪里?
    • 现在一切正常。我只是按照您的建议在我的模型中设置了 $error 属性 $this->error 。谢谢。
    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2012-12-23
    相关资源
    最近更新 更多