Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom route binding for enums doesn't work anymore #51114

Closed
larskoole opened this issue Apr 18, 2024 · 1 comment · Fixed by #51119
Closed

Custom route binding for enums doesn't work anymore #51114

larskoole opened this issue Apr 18, 2024 · 1 comment · Fixed by #51119
Labels

Comments

@larskoole
Copy link

Laravel Version

11.4.0

PHP Version

8.3.4

Database Driver & Version

No response

Description

We're using custom route binding to convert a fancy string to an int backed enum but it breaks with the new enum handling in routes.

For example https://test.test/sales-orders/... will translate sales-orders to OrderType::SALES_ORDER.
But with the new changes from #51029 this doesn't work anymore.
It tries to convert $parameterValue to a string but it's already OrderType::SALES_ORDER.

web.php

Route::get('/{orderType}');

AppServiceProvider.php

Route::bind('orderType', function ($orderType) {
    return match ($orderType) {
        'sales-orders' => OrderType::SALES_ORDER,
        default => abort(404),
    };
});

We've fixed the issue by not returning the instantiated enum but just extracting the value out of it, aka returning an int.
Would be cool if $parameterValue could be checked to see if it's already an instantiated enum.

Steps To Reproduce

Create an int backed enum

<?php

namespace App\Enums;

enum OrderType: int
{
    case SALES_ORDER = 1;
}

Add a route to the web.php

Route::get('/{orderType}');

Add model binding to the AppServiceProvider.php

Route::bind('orderType', function ($orderType) {
    return match ($orderType) {
        'sales-orders' => OrderType::SALES_ORDER,
        default => abort(404),
    };
});

Navigate to https://yourdomain.test/sales-orders

@driesvints
Copy link
Member

Thank you. I sent in a revert: #51119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants