Create Controller in Laravel

Definition

Controller is a bridge between View and Model

Location

Controller location is in app/Http/Controllers folder

How to Create

There is 2 option for creating a controller:

  1. From command line
  2. Create the code directly in a PHP file

 

From Command Line

Syntax:

php artisan make:controller YourControllerName

Example:

I want to make a Employee Controller

Question:

How do i make it … ?

Answer:

You can type this command below:

php artisan make:controller Employee

Then wait for ‘Controller created successfully’ message

Result:

Open App/Http/Controllers/Employee.php and the content look like this below:

<?php
namespace App\Http\Controllers;

class Employee extends Controller
{
//your code place
}

Directly Create From a File

Open a File then insert this code:

<?php
namespace App\Http\Controllers;

class Employee extends Controller
{
//your code place
}

Then Save it in App\Http\Controllers folder with Employee.php name

Note:

The Controller name is same as a file name

Thanks For Reading

You may also like...

Leave a Reply

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