6.7. Creating Controllers and Configuring Routing

The Laravel framework has a powerful routing subsystem. You can display your routes both for simple callback functions and for the controller methods. The simplest sample routes look like this:

  1. Route::get('/', function () {
  2. return 'Hello World';
  3. });
  4. Route::post('foo/bar', function () {
  5. return 'Hello World';
  6. });

In the first example, we register the handler of the GET request for the website root for the POST request with the route /foo/bar in the second.

You can register a route for several types of HTTP requests. For example:

  1. Route::match(['get', 'post'], 'foo/bar', function () {
  2. return 'Hello World';
  3. });

You can extract some part of the URL from the route for use as a parameter in the handling function:

  1. Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
  2. //
  3. });

The parameters of a route are always enclosed in braces.

You can find more details about routing configuration in the Routing chapter of the documentation. Routes are configured in the app/Http/routes.php file in Laravel 5.2 and in the routes/wep.php file in Laravel 5.3.