Create Your First Controller in Laravel

November 19, 2017

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

This is the directory structure of the Laravel project

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 namaspace

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');