简体   繁体   English

laravel 中路由上的隐式枚举绑定问题

[英]Problem with implicit Enum Binding on route in laravel

I have this route我有这条路线

Route::get('/post/{post:uuid}', [\App\Http\Controllers\PostController::class, 'showPost']);

And it works, if the user inputs an inexisting uuid, the app responses a 404 error, but now I want to add one more condition by using enums on route.它可以工作,如果用户输入一个不存在的 uuid,应用程序会响应 404 错误,但现在我想通过在路由上使用枚举来添加另一个条件。

I have an enum called PostStateEnum.php我有一个名为PostStateEnum.php的枚举

    <?php

namespace Modules\Muse\Enum;

use App\Http\Traits\EnumTrait;

enum PostStateEnum: string
{
    use EnumTrait;

    case DRAFT = 'draft';
    case WAITING_APPROVAL = 'waiting_approval';
    case APPROVED = 'approved';
    case REJECTED = 'rejected';
    case PUBLISHED = 'published';
    case UNPUBLISHED = 'unpublished';
}

I want to add a condition in the route: if the $post->state is PostStateEnum::PUBLISHED I want to go to the 'showPost' in my PostController我想在路由中添加一个条件:如果$post->statePostStateEnum::PUBLISHED我想去我的PostController中的 'showPost'

Currently, I'm handle that logic on my controller目前,我正在控制器上处理该逻辑

public function showPost(Post $post)
{
    if ($post->state == PostStateEnum::PUBLISHED)
    {
        dump($post);
    } else {
        return abort(404);
    }
}

According to the laravel 9 docs I understand is that I need to create another enum with only one state to be able to validate that from the route, is that correct? 根据我理解的 laravel 9 文档,我需要创建另一个只有一个状态的枚举才能从路由中验证它,对吗?

Is possible?有可能吗? Or my way is better?还是我的方法更好?

I think you are confusing what enums in the route can bring.我认为您对路线中的enums可以带来什么感到困惑。 It is not about what is already saved, but more to use it as a filter / input.这不是关于已经保存的内容,而是更多地将其用作过滤器/输入。 Imagine you want to have a route, that show posts based on status.想象一下,您想要一条路线,根据状态显示帖子。

Route::get('posts/{PostStateEnum}');

In your controller you would be able to filter based on that.在您的控制器中,您将能够基于此进行过滤。

public function index(PostStateEnum $enum) {
    if ($enum ==PostStateEnum::PUBLISHED) {
        // query filter published
    } else if ($enum ==PostStateEnum::UNPUBLISHED) {
        // query filter unpublished
    }
}

Your enum is not from the input, but from the model, therefor what you are doing is actually the correct aproach.您的枚举不是来自输入,而是来自模型,因此您所做的实际上是正确的方法。 If not done, remember to cast your enum.如果没有完成,请记住投射您的枚举。

class Post extends Model {
    protected $casts = [
        'status' => PostStateEnum::class,
    ];
}

As a more general code improvement tip, doing if else, like you did in your example is non optimal for readability, you can in these cases, reverse the if logic and do an early return approach.作为更通用的代码改进技巧,像您在示例中所做的那样执行 if else 对可读性而言不是最佳的,在这些情况下,您可以反转 if 逻辑并执行早期返回方法。

public function showPost(Post $post)
{
    if ($post->state !== PostStateEnum::PUBLISHED)
    {
        return abort(404);
    }

    return $post;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM