Home >> Laravel 5.x >> What is Controllers in Laravel 5.x

What is Controllers in Laravel 5.x

In MVC design pattern C stands for Controller.

Controller serves as an intermediary between Model and View needed to process the HTTP request.

All of your web request handling logic in a single route by routes/web.php 

Controllers are stored in the app/Http/Controllers directory.

Basic Controller

namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
 
class UserController extends Controller {
    public function show($id)
    {
        return view('user.myprofile', ['user' => User::find($id)]);
    }
}

Define Route

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

Creating a Controller by command line interface

php artisan make:controller <controller-name> --plain

Example

Step 1)-
php artisan make:controller UserController --plain

Step 2)-
You can see the created controller at app/Http/Controller/UserController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {
    public function show($id)
    {
        return view('user.myprofile', ['user' => User::find($id)]);
    }
}

Post Your Comment

Next Questions

Copyright ©2022 coderraj.com. All Rights Reserved.