Controllers in Laravel - Handling Application Logic and Data Interactions

Controllers in Laravel are classes responsible for handling the application logic and facilitating the interaction between models and views. Controllers help separate the application logic from the user interface, creating a clear and maintainable project structure.

 

Create controller

To create a controller in Laravel, you can use Laravel's Artisan command. For example, to create a controller named UserController, you can run the following command in the terminal:

php artisan make:controller UserController

Once the controller is created, you can define handling methods within the controller. For example, in the index() method, you can retrieve data from a model and pass it to a view for display:

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();

        return view('users.index', ['users' => $users]);
    }

    // Other handling methods
}

In the above example, we use the User model to retrieve user data from the database. We then pass this data to the users.index view to display a list of users.

Controllers also support methods such as store(), update(), and delete() to handle data creation, updating, and deletion. You can interact with the database through these methods.

 

HUsing controller in route

To use a controller in route, you can specify the controller name and the corresponding method in the routes/web.php file.

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

In this example, when a user accesses the /users URL, Laravel will call the index() method in the UserController to handle the request.

 

Create a view for the user list screen

To create the users.index file, you can use the following command:

php artisan make:view users.index

This command will create an index.blade.php file in the resources/views/users directory.

Once the file is created, you can open the index.blade.php file and design the interface for the users.index page. You can use Blade syntax to create the HTML structure and display data from the controller.

<!-- resources/views/users/index.blade.php -->
@extends('layouts.app')

@section('content')
    <h1>Users</h1>
    <ul>
        @foreach ($users as $user)
            <li>{{ $user->name }}</li>
        @endforeach
    </ul>
@endsection

In the example above, we use the app.blade.php layout through @extends('layouts.app'). The content of the page is defined within @section('content') and displays a list of users from the $users variable within a @foreach loop.

To use the users.index page, you need to define the corresponding route in the routes/web.php file to point to the method in the controller and return the users.index view.

 

In summary, controllers in Laravel help separate the application logic and handle data processing. By using controllers, you can build powerful and maintainable applications in Laravel.