Create Your First Controller in Laravel

Last Updated: July 12, 2021

In this lesson, we will see how to create your first controller in Laravel. If you do not know how to install Laravel on your computer you can see this tutorial.

This is the directory structure of the Laravel project

Laravel Directory Structure

So you can create your controller App > Http > Controllers

You can run following command inside the terminal.

php artisan make:controller UserController 

If you want to create the controller with model you can use the following code.

php artisan make:controller UserController --model=User 

Above artisan command will create the following code in App\Http\Controllers namespace.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    //
}

Now I will create the show() inside the UserController

class UserController extends Controller
{
    //

    public function show(){
    	echo "User Details";
    }
}

Now you can define the route for this controller action in routes>web.php file

Route::get('user/{id}', 'UserController@show');