Book Image

Laravel 5 Essentials

By : Martin Bean
Book Image

Laravel 5 Essentials

By: Martin Bean

Overview of this book

Table of Contents (15 chapters)
Laravel 5 Essentials
About the Author
About the Reviewers

Writing the first routes

Let's start by writing the first two routes of our application at app/Http/routes.php. This file already contains some comments as well as a couple of sample routes. Remove the existing routes (but leave the opening <?php declaration) before adding the following routes:

Route::get('/', function() {
  return 'All cats';

Route::get('cats/{id}', function($id) {
  return sprintf('Cat #%s', $id);

The first parameter of the get method is the URI pattern. When a pattern is matched, the closure function in the second parameter is executed with any parameters that were extracted from the pattern. Note that the slash prefix in the pattern is optional; however, you should not have any trailing slashes. You can make sure that your routes work by opening your web browser and visiting

Restricting the route parameters

In the pattern of the second route, {id} currently matches any string or number. To restrict it so that it only matches numbers...