php - 美化代码结构的方法是什么? (拉拉维尔)

标签 php laravel

我开始编写类似电子商务之类的新网站,但这只是一个评论网站,用户对品牌、产品和品牌帖子发表评论。所以,我有一个用于注释的多态表。

当有人尝试添加评论时,首先,我需要定义评论类型,例如品牌、产品或帖子。在本例中,我使用 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();
    }
}

$请求=输入

$type = commentable_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的路由器将立即看到您要添加评论的可评论实体的类型。路由器还将利用 implicit model binding并将根据指定的 id 为您找到这些模型(如果您的数据库中不存在所述行,则返回 404),因此我们也摆脱了那些讨厌的 findOrFail 调用。

现在在你的 Controller 中你应该使用 form requests for validation (而不是手动创建 Validator 实例)。最后,我们可以将创建新 Comment 实例的逻辑(对于所有可注释类型来说很常见)分组到单独的方法中。然后你的 Controller 将如下所示:

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();
}

...

方法 addProductCommentaddBrandComment 不会有太大不同。

关于php - 美化代码结构的方法是什么? (拉拉维尔),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56168169/

相关文章:

php - 如何根据数据库下拉列表的 mysql 计数来限制或停止表单提交?

php - 如何根据 session 中的值使用 Assetic 嵌入样式表

laravel - Nginx - 将非 ssl 和非 www 重定向到 ssl 和 www

php - 对于 Laravel Eloquent 模型及其关系,如何实现由 UUID 组成的主键,而不是自动递增的整数?

php - Laravel 身份验证 session cookie 未发送

php - 在没有括号表示法的情况下访问 PHP 中的多个 GET 参数

php - PHP/MySQL 应用程序声明外键是一种常见的做法吗?

php - 以特定格式显示MySQL数据

sql - 如何将 Laravel 迁移转换为原始 SQL 脚本?

laravel - With()中如何写子查询?