ItSolutionStuff.com

How to Create Controller in Laravel using Artisan Command?

By Hardik Savani • April 16, 2024
Laravel

In this article, i will show you how to create controller in laravel application. i will give you examples of create controller in laravel using cmd. we will use php artisan make controller command to create controller in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

You have to see this tutorial for creating controller in laravel using artisan command. you can easily create controller with windows cmd and terminal.

What is Controller in Laravel?

In Laravel Controller handle all request of routes files and write logic for Views and Models. Using Controller you can easily bind models and views logic on it.

You can easily create controller using laravel command.

Now we will see how to create controller on laravel. You can simply create controller by following command:

php artisan make:controller DemoController

Now you can see controller file on bellow path:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

}

You can easily connect route with controller as like bellow:

routes/web.php

Route::get('demo', 'DemoController@index');

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

return view('demo');

}

}

You can also create controller with invokable controller using bellow command:

php artisan make:controller ShowProfile --invokable

app/Http/Controllers/ShowProfile.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ShowProfile extends Controller

{

/**

* Handle the incoming request.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function __invoke(Request $request)

{

}

}

You can easily create resource controller. I already written tutorial on it. You can see that by clicking here: Resource Controller.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Custom Attribute in Laravel Model?

Read Now →

How to Pass Data from Controller to View in Laravel?

Read Now →

How to use Carbon in Laravel Blade or Controller File?

Read Now →

How to Call Controller Function in Blade Laravel?

Read Now →

Laravel Redirect to Route from Controller Example

Read Now →

How to Call a Controller Function in Another Controller in Laravel?

Read Now →

Laravel Call Function from Same Controller Example

Read Now →

Laravel Include Blade File with Data Example

Read Now →

Laravel Blade Include File Example

Read Now →

How to Create Reusable Components in Laravel?

Read Now →

Laravel - How to Get .env Variable in Blade or Controller?

Read Now →

How to Call Middleware from Controller in Laravel?

Read Now →

How to Create Resource Controller in Laravel?

Read Now →

How to Execute Artisan Command from Controller in Laravel?

Read Now →

How to Get Current URL in Laravel?

Read Now →