Laravel Tutorial - Laravel Controllers

Published January 29, 2022

In Laravel applications, controllers are significant because they direct traffic between the models and views. In this chapter, we will discuss Laravel controllers. We are going to look at:

  • How to create a Controller

  • The controller Middleware

  • The Restful Resource Controllers

  • The Implicit Controllers

 

1. How to create a Controller

Creating a Laravel application controller is pretty easy. Just follow the steps below:

 

Step 1:Open your windows command prompt  and run the command below  to create a new User Controller

 

php artisan make:controller UserController –plain

 

Laravel Controller example

 

On your command prompt, you will see a message reading, "Controller created successfully."

This command will create a plain constructor upon passing the argument; however, it is also possible to avoid creating a plain constructor. To do so, just ignore the argument.

Your constructor will be created in the app/Http/Controllers directory

 

Laravel Controller example2

 

Step 2. You can now customise your controller. Some basic coding is already added inside our controller, but this does not restrict you from adding your own code to meet your specifications. 

The created laravel controller image

 

Laravel Controller example3

 

The created controller generated Code

<?php

namespace App\Http\Controllers;

use App\Models\lain;

use Illuminate\Http\Request;

use {{ namespacedModel }};

 

class UserController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @param  \App\Models\lain  $lain

     * @return \Illuminate\Http\Response

     */

    public function index(lain $lain)

    {

        //

    }

 

    /**

     * Show the form for creating a new resource.

     *

     * @param  \App\Models\lain  $lain

     * @return \Illuminate\Http\Response

     */

    public function create(lain $lain)

    {

        //

    }

 

    /**

     * Store a newly created resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \App\Models\lain  $lain

     * @return \Illuminate\Http\Response

     */

    public function store(Request $request, lain $lain)

    {

        //

    }

 

    /**

     * Display the specified resource.

     *

     * @param  \App\Models\lain  $lain

     * @param  \{{ namespacedModel }}  ${{ modelVariable }}

     * @return \Illuminate\Http\Response

     */

    public function show(lain $lain, {{ model }} ${{ modelVariable }})

    {

        //

    }

 

    /**

     * Show the form for editing the specified resource.

     *

     * @param  \App\Models\lain  $lain

     * @param  \{{ namespacedModel }}  ${{ modelVariable }}

     * @return \Illuminate\Http\Response

     */

    public function edit(lain $lain, {{ model }} ${{ modelVariable }})

    {

        //

    }

 

    /**

     * Update the specified resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \App\Models\lain  $lain

     * @param  \{{ namespacedModel }}  ${{ modelVariable }}

     * @return \Illuminate\Http\Response

     */

    public function update(Request $request, lain $lain, {{ model }} ${{ modelVariable }})

    {

        //

    }

 

    /**

     * Remove the specified resource from storage.

     *

     * @param  \App\Models\lain  $lain

     * @param  \{{ namespacedModel }}  ${{ modelVariable }}

     * @return \Illuminate\Http\Response

     */

    public function destroy(lain $lain, {{ model }} ${{ modelVariable }})

    {

        //

    }

}

For this tutorial, we will not customise it.

 

1. The controller Middleware

Middlewares can also be employed in controllers, where they are assigned to the controller's routes or contructors. Simply use the middleware method to add the middleware to the controller. You may also restrict the middleware to a certain controller function. Let's take a look at how to add middleware to a route or the constructor of a controller.

 

2. The Restful Resource Controllers

When we build a web application, we generally require it to do CRUD (Creating, Retrieving, Editing, and Dropping) actions. CRUD procedures have been simplified in Laravel 8. All we need to do is construct a controller, and Laravel will generate all of the CRUD operation methods for us.

 

3. The Implicit Controllers

Implicit controllers enable us to build a single route to handle all controller activities. The route.php file defines this route. To do this, just add a Route: controller method using the Syntax below

Route::controller(‘base URI’,’ <name-of-controller-class>’);

 

Where <name-of-controller-class> is the class name that you would like to give to your controller.

Conclusion

Finally, we’ve looked at the Laravel controllers. We've learned how to create Laravel controllers. Furthermore, we have examined the controller middleware, the restful resource controllers, and the implicit controllers

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

160 Views