-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,332 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Implicit enum binding | ||
|
||
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](mappers.md) on | ||
both basic as string and int backed enums as well. | ||
|
||
## binding a simple enum | ||
|
||
````php | ||
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' | ||
|
||
```` | ||
|
||
## binding optionally | ||
|
||
````php | ||
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' | ||
|
||
```` | ||
|
||
## binding optionally with default | ||
|
||
````php | ||
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 | ||
|
||
Binding an int-backed enum to your routes is just as easy as with basic enums. | ||
|
||
````php | ||
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' | ||
|
||
```` | ||
|
||
## Binding a string-backed enum | ||
|
||
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. |
Oops, something went wrong.