Cover Image for Laravel Controllers
165 views

Laravel Controllers

Laravel controllers are an essential part of the framework, responsible for handling incoming HTTP requests and returning appropriate responses. Controllers act as intermediaries between your application’s routes and the logic required to process and respond to requests. They help you organize your application’s logic into manageable and reusable components.

Here’s how to create and use controllers in Laravel:

1. Creating a Controller:

You can create a new controller using the Laravel Artisan command-line tool. Open your terminal and navigate to your Laravel project directory. Then, run the following command to generate a new controller:

Bash
php artisan make:controller MyController

Replace “MyController” with your desired controller name. This command will create a new controller file in the app/Http/Controllers directory.

2. Defining Controller Methods:

Inside your controller file, you can define methods that correspond to different actions or routes. For example, you might have a method to display a page and another to process a form submission. Here’s an example of a simple controller with two methods:

PHP
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    public function show()
    {
        return view('myview');
    }

    public function store(Request $request)
    {
        // Process form submission data
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users',
            // Add more validation rules as needed
        ]);

        // Save data to the database
        // ...

        return redirect()->route('myview')->with('success', 'Data saved successfully!');
    }
}

3. Routing to Controller Actions:

To route incoming requests to your controller methods, you need to define routes in the routes/web.php file or routes/api.php file. Here’s how you can route to the show and store methods in your controller:

PHP
use App\Http\Controllers\MyController;

// Route to the "show" method
Route::get('/myroute', [MyController::class, 'show'])->name('myview');

// Route to the "store" method with POST request
Route::post('/myroute', [MyController::class, 'store']);

In this example, we use the Route::get and Route::post methods to define routes for the show and store methods, respectively.

4. Handling Request Data:

You can access request data, such as form input, query parameters, and route parameters, in your controller methods by type-hinting the Illuminate\Http\Request class or other request-specific classes. For example, in the store method above, we type-hinted Request $request to access form data.

5. Returning Responses:

In your controller methods, you can return various types of responses, including views, JSON responses, redirects, and more. In the show method above, we returned a view using return view('myview'), while in the store method, we returned a redirect response with a flash message.

Laravel controllers help you structure your application logic and keep it organized. They also make it easy to separate concerns by placing specific functionality into separate controller methods. This separation of concerns contributes to the maintainability and scalability of your Laravel application.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS