隐式枚举绑定在Laravel的路线上的问题

发布于 2025-02-08 05:31:51 字数 1203 浏览 1 评论 0 原文

我有这条路线

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

,它可以正常工作,如果用户输入一个没有uuID,则应用程序会响应404错误,但是现在我想通过在路由上使用枚举来添加更多条件。

我有一个名为 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';
}

我想在路由中添加条件:如果 $ post- post-&gt; state is poststateenum ::已发布我想在我的 PostController 中转到“ showpost”

,目前,我在控制器上处理该逻辑

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

我知道,我需要只有一个状态来从路线上验证这一点,/ruting#nimit-enum绑定“ rel =“ rel =” nofollow noreferrer”>那是对的吗?

可能吗?还是我的方式更好?

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.

I have an enum called 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

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?

Is possible? Or my way is better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2025-02-15 05:31:51

我认为您在路线上会带来什么枚举。它不是关于已经保存的内容,而是将其用作过滤器 /输入。想象一下,您想拥有一条路线,该路线根据状态显示帖子。

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

在您的控制器中,您将能够基于此过滤。

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

您的枚举不是来自输入,而是来自模型,因此您正在做的实际上是正确的侵犯。如果没有完成,请记住要投入枚举。

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

作为一个更一般的代码改进提示,如果您在示例中所做的像可读性不是最佳的事情,那么在这些情况下,您可以扭转IF逻辑并进行早期返回方法。

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

    return $post;
}

I think you are confusing what enums in the route can bring. 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.

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

    return $post;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文