Laravel

Laravel Controller with Example

January 30, 2018

author:

Laravel Controller with Example

As we know, Laravel is an MVC Framework. Though we can create routes and directly render required data, it doesn’t really serve the purpose of MVC architecture. MVC aims at decentralizing the coding patterns. And thus, controllers come in the picture. A controller in Laravel performs the same task as discussed in MVC section. But as a reminder – Controller is the only link between Model and View.

# How to create Controller in Laravel?

Creating new controller in Laravel is just about running a simple artisan command at the command line prompt:

Syntax:

php artisan make:controller <controller_name>

Using above syntax, let us now create a controller:

php artisan make:controller PublicController
# app/Http/Controllers/PublicController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PublicController extends Controller
{
    //
}

As you can see, creating controller from artisan provides the basic scaffolding of the controller and we can directly start adding our custom code.

# How to Link Route and Controller?

As a next step, let us now create a new method in PublicController for about page.

# app/HttpControllers/PublicController.php
...
class PublicController extends Controller
{
    public function about(){
        return 'About Page from Controller';
   }
}

Incase you test this in the browser, at http://localhost:8000/about, you wouldn’t notice any change in the output?

Why?
The reason is very simple. We did create a fresh controller and method to display about page data, but we have missed a really important step. Integrating this PublicController with our web.php. So let us proceed and complete that.

Route::get('about', 'PublicController@about');

Also note here that to link controller in web.php, we didn’t have to reference PublicController namespace as App\Http\Controllers\PublicController since it inhabits in the root directory of Controllers. But if we had controller class in a nested structure – App\Http\Controllers\Admin\DashboardController.php, we would have to link this controller in web.php as:

route::get('admin/dashboard', 'Admin\DashboardController@method');

Okay, so let us test our about page now..

http://localhost:8000/about
Laravel Controller Example

# Invoke Method – Single Action in Laravel Controller

Let us discuss one less popular yet useful way of declaring methods in the controller. You may come across a situation where you need only one method but in a separate controller. As we have in our example. If in our PublicController we don’t intend to add any more methods except about, we can declare in this way too:

# app/HttpControllers/PublicController.php
...
class PublicController extends Controller
{
    public function __invoke(){
        return 'About Page from Controller';
    }
}

And then register its route as:

# routes/web.php

Route::get('about', 'PublicController');

You’ll notice no change in the resultant output.

Conclusion:

In this segment, we had a quick but deep understanding of Laravel controllers and its usability. In Laravel Project we can have as many controllers and methods as required. But segregating them in such multiple controllers in some logical order is crucial for maintainability of the project. So, go ahead, think of some complex use case for yourself and try coding them down.

Once you work on multiple Laravel projects, you’ll notice that many mandatory methods are repeated in all of them. For example CRUD operations. Most of the projects are backed on that. How about not having to write such methods again and again? How about not having to link such CRUD functions to routes again and again for each component? What if I told you Laravel has got that covered? Excited enough? In the next segment, we will see how that is possible. Stay tuned.

Questions & Comments:

Happy to learn? If you get stuck with Controller in Laravel at any stage or have questions, comment them below.

Leave a comment

Your email address will not be published. Required fields are marked *