Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Created September 11, 2016 09:27
Show Gist options
  • Select an option

  • Save prodeveloper/79f69dcabb1aa2d509db0aacb1524c9d to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper/79f69dcabb1aa2d509db0aacb1524c9d to your computer and use it in GitHub Desktop.
Introduction to using implicit controllers in laravel

In the past classes we have been doing all of our routes via the route.php file.

This tactic however does not scale well when you are building a production level application.

Controllers help you organize your routes into meaningful chunks. In addition to better organization, you can now work with the routes as a collective.

To illustrate we will move student routes to a controller called StudentController

##Step 1

Lets create the controller by running the following command from the terminal.

php artisan make:controller StudentController

Navigate to app/Http/Controllers

You should see a new file called StudentController . The file contains a code stub that we will now extend with our functionality.

##Step 2

Now that we have our controller lets now transfer functionality to the controller.

Here is how the route looks like.

Route::get('/students', function () {
    return view('students',['students'=>App\Student::all()]);
  
});


Route::get('/students/add', function () {
    return view('new_student');
});

Route::get('/students/{id}/edit',function($id){
    $student=App\Student::findOrFail($id);
    return view('edit_student',['student'=>$student]);
});
Route::post('/students/{id}/edit',function($id){
    $student=App\Student::findOrFail($id);
    $student->name= Request::get('name');
    $student->email= Request::get('email');
    $student->course= Request::get('course');
    $student->save();
    return Redirect::to('/students');
});

Route::post('/students/add', function () {
    //Create a new student object as we did in the tinker terminal
    $student = new App\Student();
    $student->name= Request::get('name');
    $student->email= Request::get('email');
    $student->course= Request::get('course');
    $student->save();
    //Once saved redirect back to all students table
    return Redirect::to('/students');
});

We now create methods that mirror that functionality in our Controller class as follows.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App;

class StudentController extends Controller
{
    function getAll(){
         return view('students',['students'=>App\Student::all()]);
    }
    function getAdd(){
        return view('new_student');
    }
    function postAdd(){
             //Create a new student object as we did in the tinker terminal
        $student = new App\Student();
        $student->name= Request::get('name');
        $student->email= Request::get('email');
        $student->course= Request::get('course');
        $student->save();
        //Once saved redirect back to all students table
        return Redirect::to('/students');
    }
    function getEdit(){
        $student=App\Student::findOrFail($id);
        return view('edit_student',['student'=>$student]);
    }
    function postEdit(){
        $student=App\Student::findOrFail($id);
        $student->name= Request::get('name');
        $student->email= Request::get('email');
        $student->course= Request::get('course');
        $student->save();
        return Redirect::to('/students');
    }
    
}

You may notice that the functionality is exactly the same. The prefix get and post are very important. They denote the verb that the method will respond to.

Note that we added the statement "use App" to ensure that we can reference methods in our functionality. See Dayle explanation of namespaces here PHP Namespaces Explained

##Step 3

With the functionality now moved to the controller, we need to tell laravel through the routes.php file that it should now look for functionality at the controller file. We do this by adding the line

Route::controller('students','StudentController');

To the routes.php file.

We can now delete all the routing closure from the routing file.

##Step 4

You can test out all the controllers to ensure the work.

The routing method we used above is called implicit controllers. It provides intelligent routing so that you don't need to explicitly define all the routes. As long as you have named it right. Laravel will take care of the rest for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment