Php 如何美化代码结构?(拉雷维尔)

Php 如何美化代码结构?(拉雷维尔),php,laravel,Php,Laravel,我开始编写新网站的代码,比如电子商务,但它只是一个评论网站,用户可以对品牌、产品和品牌帖子发表评论。所以,我有一个多态的评论表 当有人试图添加评论时,首先,我需要定义评论类型,如品牌、产品或帖子。在本例中,我使用switch case来了解用户想要做什么。我认为有更好的方法可以用干净的代码结构来实现这一点,这就是我在这里的原因 我只是想知道这是否是正确的方式添加评论如下 public function addComment(Request $request, $type, $id, $tab =

我开始编写新网站的代码,比如电子商务,但它只是一个评论网站,用户可以对品牌、产品和品牌帖子发表评论。所以,我有一个多态的评论表

当有人试图添加评论时,首先,我需要定义评论类型,如品牌、产品或帖子。在本例中,我使用switch case来了解用户想要做什么。我认为有更好的方法可以用干净的代码结构来实现这一点,这就是我在这里的原因

我只是想知道这是否是正确的方式添加评论如下

public function addComment(Request $request, $type, $id, $tab = null)
{
    // Error messages
    $messages = [
        'add_comment.required' => '...',
        'add_comment.min' => '...',
        'add_comment.max' => '...',
        'rating.numeric' => '...',
        'rating.min' => '...',
        'rating.max' => '...'
    ];

    // Validate the form data
    $validator = Validator::make($request->all(), [
            'add_comment' => 'required|min:5|max:2000',
            'rating' => 'numeric|min:0|max:5'
        ], $messages);

    if($validator->fails())
    {
        return back()->withErrors($validator);  
    } else {
        $comment = new Comment;
        $comment->body = $request->get('add_comment');
        $comment->user()->associate(Auth::user()->id);
        $comment->star_value = $request->get('rating');

        switch ($type) {
            case 'Post':
                $post = Post::findOrFail($id);
                $comment->star_value = NULL;
                $post->comments()->save($comment);
                break;
            case 'Product':
                $product = Product::findOrFail($id);
                $product->comments()->save($comment);

                //Update rating of product
                $average = $product->comments()->getAvg();
                $product->rating = $average;
                $product->save();
                break;
            default:
                $this->postCommentToBrand($comment, $id, $tab);
                break;
        }                     

        return redirect()->back();
    }
}
$request=输入

$type=可评论类型(品牌、产品、帖子)

$id=$type的id


$tab=这实际上是针对品牌的。因为品牌有客户支持和技术支持。还需要使用switch case来定义它。

将它拆分为单独的路由-每个可注释类型一个路由,例如:

Route::post('add-comment/post/{post}', 'CommentsController@addPostComment');
Route::post('add-comment/product/{product}', 'CommentsController@addProductComment');
Route::post('add-comment/brand/{brand}/{tab}', 'CommentsController@addBrandComment');
这将处理好您的
交换机
——现在,Laravel的路由器将立即看到您正在添加注释的可注释实体的类型。路由器还将利用并通过指定的id为您查找这些型号(如果数据库中不存在所述行,则返回404),因此我们也可以摆脱那些讨厌的
findOrFail
调用

现在,您应该在控制器中使用(而不是手动创建
验证程序
实例)。最后,我们可以将创建新的
Comment
实例(对于所有可注释类型来说都是通用的)的逻辑分组到单独的方法中。然后,您的控制器将如下所示:

protected function getNewCommentFromRequest(Request $request)
{
    $comment = new Comment;
    $comment->body = $request->get('add_comment');
    $comment->user()->associate(Auth::user()->id);
    $comment->star_value = $request->get('rating');

    return $comment;
}

public function addPostComment(AddCommentRequest $request, Post $post)
{
    $comment = $this->getNewCommentFromRequest($request);
    $comment->star_value = NULL;
    $post->comments()->save($comment);

    return redirect()->back();
}

...

方法
addProductComment
addBrandComment
不会有太大区别。

这正是我所期待的。非常感谢。我是否需要类似于您的回复意见的答案?你能给我举个例子吗。我做了,但我不确定这是否完美。你可以在评论中问我。如果你觉得问题太大或与当前问题无关,那么将其作为单独的问题发布。尽管我需要提醒你们,世上并没有“完美的代码”或“完美的解决方案”。一般做法是使代码更易于维护和进行无害的更改。但有时候做得太多可能是一种倒退。是的,我同意你的看法。如果有类似完美代码的东西,我们可以很容易地找到:)谢谢您的回复。现在不需要单独的问题。我刚刚从脑海中得到了关于评论回复的答案^_^