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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Moving from simple routing to powerful controllers


So far, we have been creating closure-based routes. This is great for quickly prototyping applications, and is prevalent in micro-frameworks such as Silex and Slim; however, as your application grows, this approach might become cumbersome and limiting. The alternative (and recommended) approach to defining the logic to be executed when a route is requested is in controllers, the C in MVC.

A controller is usually a class, containing one or more methods, also known as actions. You usually have a route map to a controller action.

Consider the following example:

Route::get('user/{id}', ['middleware' => ['auth'], function($id) {
  // Perform some operations
  return 'Something';
}]);

To achieve the same functionality with a controller and remove the business logic from the routes, create a new file at app/Http/Controllers/UserController.php:

<?php namespace Furbook\Http\Controllers;

class UserController extends Controller {
  public function...