Laravel already supports enum binding for routes out of the box. Enumhancer just adds some of it's famous secret flavor to level up it's potential.
This feature is loaded out of the box and does not alone allow you to bind basic enums, but also allows you to use features like Mappers on both basic as string and int backed enums as well.
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;
}
Illuminate\Support\Facades\Route::get(
'/card/{card}',
function (Suit $card) {
print $card->name;
}
);
/card/Hearts // prints 'Hearts'
/card/diamonds // prints 'Diamonds'
/card/0 // prints 'Hearts'
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;
}
Illuminate\Support\Facades\Route::get(
'/card/{card?}',
function (Suit $card = null) {
print $card->name ?? 'nothing';
}
);
/card/Hearts // prints 'Hearts'
/card/ // prints 'nothing'
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;
const Default = Suit::Clubs;
}
Illuminate\Support\Facades\Route::get(
'/card/{card?}',
function (Suit $card) {
print $card->name;
}
);
/card/Hearts // prints 'Hearts'
/card/ // prints 'Clubs'
Binding an int-backed enum to your routes is just as easy as with basic enums.
enum Suit: int {
case Hearts = 1;
case Diamonds = 5 ;
case Spades = 10 ;
case Clubs = 15;
}
Illuminate\Support\Facades\Route::get(
'/card/{card}',
function (Suit $card) {
print $card->name;
}
);
/card/Hearts // prints 'Hearts'
/card/diamonds // prints 'Diamonds'
/card/0 // prints 'Hearts'
/card/15 // prints 'Clubs'
Laravel has its own enum binding in place for string-backed enums. Under the hood, Enumhancer is just adding another middleware and gives it priority over Laravel's binding middleware. When a string-backed enum is used, Enumhancer maps the given value if needed and replaces the requested parameter value with the correct value.