LARAVEL Tutorial


Laravel 7 Controller Tutorial


You have learned in the previous tutorial, what is laravel? how to install and configure laravel, etc. In the previous tutorial, we also learned laravel is MVC pattern based PHP frameworks. Here C stands for the controller.

Laravel is a free and open source MVC pattern based PHP framework. Here C stand for the controller. Laravel controllers interact with views and models simultaneously. When an HTTP request is made to the laravel web application, the route receives the request then calls a method of the controller. The controller then communicates with the model to perform business logic. The controller method then loads the required views when needed.

A controller is located in the app/Http/Controllers directory. You can create a new controller in laravel with php Artisan command make:controller using command line (cmd).

Contents - Laravel Controllers

  • Types of Controllers
  • Create a Controller
  • Controller Middleware

Types of Controllers

Laravel has provided the following type of controllers:

Basic Controller - This is a basic class. When it is created from the command line (CLI). So there is no method in it.

Resource Controller - When it is created from the command line (CLI). So it has 5 method inbuilt named index(), store(), edit(), update(), delete().

API Controller - This type of controller is similar to a resource controller, except that the API does not exist in the way the controller displays the forms.

Create a Controller

Here, you will learn how to create basic, resource and API controllers using php artisan:make controller in laravel with command line (cmd):

Create Basic controller

Here, Just Open the command line and go to your laravel web application root directory. And type the following command for make controller in laravel using cmd: 

php artisan make:controller PostController

After that, you can view your created controller by going to "App/Http/Controllers". Your controller will look like the following:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    //
}

If you want to create a controller inside a folder, You can use the following command:

//php artisan make the controller in folder

php artisan make:controller User\PostController

Create Resource Controller

A resource controller is used to perform crud(create, read, update and delete) operation from the database. 

Here, Just Open the command line and go to your laravel web application root directory. And type the following command to creating a resource controller

php artisan make:controller controllerName --resource command

php artisan make:controller TestController --resource

After that, you can view your created controller by going to "App/Http/Controllers". Your controller will look like the following:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Let's now add the corresponding route for created resource controller

Route::resource('tests', 'TestController');

Create API Controller

Nowadays, APIs are very popular things in Web applications. Which is used to allow the system to communicate with another one.

How to create api controller in laravel?

Here, Just Open the command line and go to your laravel web application root directory. And type the following command to creating a resource controller

php artisan make:controller API/OfferController --api

After that, you can view your created controller by going to "App/Http/Controllers/API". Your controller will look like the following:

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OfferController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Let's now update the api.php routes file located in /routes directory like so

Route::apiResource('offers', 'API\OfferController');

Controller Middleware

In the previous tutorial, we have learned about laravel middlewares. If you are new here, please refer to the previous tutorial laravel middleware tutorial.

We will use the middleware on PostController class:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        return 'home page';
    }
}

HERE,

  • public function __construct(){...} :- defines a constructor method for our controller.
  • $this->middleware('auth'); :- First, the condition of the middleware will be checked then the method of the PostController will be execute