How to Create a New Controller with Default Resource Template in Laravel

Posted on

Introduction

This is an article for creating a new controller. The controller will be available and exist in the Laravel-based project or application. It is useful as definition for controlling process in the Laravel-based project or application. But in this context, since Laravel is a PHP framework for developing project or application, it will be convenient and comfortable to have a controller with a default template. In this case, a default template is in the design pattern of  Create, Update and Delete process namely known as CRUD.

Create a New Controller with Default Resource Template in Laravel

Since Laravel is a framework, it has several tool and utility to ease the developer in building the project or application. One of those are the ability to generate controller with a default template. Actually, the process is quite simple. The following are the steps to do it :

  1. First of all, having a Laravel-based project or application is a must. For reference, read the article ‘How to Create a Laravel Project using Composer in Microsoft Windows’ in this link to know how to be able to generate it.

  2. After that, access the root folder of the Laravel-based project or application. In this case, for an example, it exist in ‘C:\programming\php\laravel\crud’.

  3. Soon after, execute the following command in that path :

    php artisan make:controller controller_name --resource
  4. The above command will generate a new file in ‘app\Http\Controller’ with the name of ‘EmployeeController.php’. Before that, just try to look at the default content of the directory first :

    C:\programming\php\laravel\crud\app\Http\Controllers>dir
    Volume in drive C is Windows
    Volume Serial Number is E003-3593
    
    Directory of C:\programming\php\laravel\crud\app\Http\Controllers
    
    08/01/2021 04:50 PM <DIR> .
    08/01/2021 04:50 PM <DIR> ..
    07/13/2021 09:12 PM 361 Controller.php
    2 File(s) 1,952 bytes
    2 Dir(s) 182,824,960 bytes free
    
    C:\programming\php\laravel\crud\app\Http\Controllers>

    At first, there is only one file with the name of ‘Controller.php’.

  5. In order to add a new controller with the name of ‘EmployeeController’, just execute the command according to the above command pattern as follows :

    C:\programming\php\laravel\crud> php artisan make:controller EmployeeController --resource
    Controller created successfully.
    C:\programming\php\laravel\crud> 
    
  6. By executing the command, there will be an appearance of a new file with the name of EmployeeController as follows :

    C:\programming\php\laravel\crud\app\Http\Controllers>dir
    Volume in drive C is Windows
    Volume Serial Number is E003-3593
    
    Directory of C:\programming\php\laravel\crud\app\Http\Controllers
    
    08/01/2021 04:50 PM <DIR> .
    08/01/2021 04:50 PM <DIR> ..
    07/13/2021 09:12 PM 361 Controller.php
    08/01/2021 05:02 PM 1,591 EmployeeController.php
    2 File(s) 1,952 bytes
    2 Dir(s) 182,824,960 bytes free
    
    C:\programming\php\laravel\crud\app\Http\Controllers>
    

    Since there is a new file appear with the name of ‘EmployeeController’, the command is a success. The following is the actual content of the file :

    <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    class EmployeeController 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)
        {
           //
        }
    }

    As the above command is producing the right file with the right content, the command execution is a success.

Leave a Reply