0% found this document useful (0 votes)
4 views

Controllers

Uploaded by

robim60752
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Controllers

Uploaded by

robim60752
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CONTROLLERS

Akash Pundir
Assistant Professor
System Programming -I
• In Laravel, a controller is a crucial
component used to handle HTTP
requests and define the application's
response logic. Controllers serve as
an intermediary between routes and
the actual business logic of your
application.
Controllers
• Instead of defining all of your request handling logic as
closures in your route files, you may wish to organize this
behavior using "controller" classes.

• Controllers can group related request handling logic into a


single class.

• For example, a UserController class might handle all


incoming requests related to users, including showing,
creating, updating, and deleting users.

• By default, controllers are stored in the app/Http/Controllers


directory.
Writing Controllers
Basic controllers –

To quickly generate a new controller, you may run the


make:controller Artisan command. By default, all of the
controllers for your application are stored in the
app/Http/Controllers directory:

php artisan make:controller


UserController
Writing Controllers(contd.)
• You can define a route to this controller method like so:

use App\Http\Controllers\UserController;

Route::get('/user/{id}', [UserController::class, 'show']);

• When an incoming request matches the specified route


URI, the show method on the App\Http\Controllers\
UserController class will be invoked and the route
parameters will be passed to the method.
Writing Controllers(contd.)
• Single Action Controllers - If a controller action is
particularly complex, you might find it convenient to
dedicate an entire controller class to that single action. To
accomplish this, you may define a single __invoke method
within the controller:
Writing Controllers(contd.)
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ProvisionServer extends Controller


{
public function _ _invoke()
{
// ...
}
}
Writing Controllers(contd.)
• When registering routes for single action controllers, you
do not need to specify a controller method. Instead, you
may simply pass the name of the controller to the router:

use App\Http\Controllers\ProvisionServer;

Route::post('/server', ProvisionServer::class);
Writing Controllers(contd.)
• You may generate an invokable controller by using
the --invokable option of the make:controller Artisan
command:

php artisan make:controller ProvisionServer --


invokable
Dependency Injection & Controllers
• A common use-case for method injection is injecting the
Illuminate\Http\Request instance into your controller
methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$name = $request->name;
//
}
}
Dependency Injection
• It allows the creation of
dependent objects outside of a
class and provides those objects
to a class through different ways.
Using DI, we move the creation
and binding of the dependent
objects outside of the class that
depends on them.
Dependency Injection & Controllers(contd.)
• If your controller method is also expecting input from a
route parameter, list your route arguments after your other
dependencies. For example, if your route is defined like
so:

use App\Http\Controllers\UserController;

Route::put('/user/{id}', [UserController::class, 'update']);


Dependency Injection & Controllers(contd.)
• You may still type-hint the Illuminate\Http\Request and
access your id parameter by defining your controller
method as follows:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function update(Request $request, $id)
{
//
}
}

You might also like