Using advanced routing
When creating routes that require parameters, we may need to use more advanced features. Using Laravel and regular expressions, we can make sure that our routes only respond to certain URLs.
Getting ready
For this recipe, we need a standard Laravel installation.
How to do it…
To complete this recipe, follow these steps:
In our
routes.php
file, add the following code:Route::get('tvshow/{show?}/{year?}', function($show = null, $year = null) { if (!$show && !$year) { return 'You did not pick a show.'; } elseif (!$year) { return 'You picked the show <strong>' . $show . '</strong>'; } return 'You picked the show <strong>' . $show .'</strong> from the year <em>' . $year . '</em>.'; }) ->where('year', '\d{4}');
Open a browser and test the route by typing something such as
http://your-server/tvshow/MASH/1981
(whereyour-server
is the URL for your server) in the address bar.
How it works...
We start by having our...