Controllers
Introduction
Rather than defining all request-handling logic as closures in route files, you can use controller classes to organize related functionality. Controllers centralize request handling, making your code more structured and maintainable. For example, a UserController can manage user-related actions like displaying, creating, updating, and deleting users. By default, controllers are stored in the app/Http/Controllers directory.
Create Controller
To quickly generate a new controller, you may run the make:controller
Pool command. By default, all of the controllers for your application are stored in the app/Http/Controllers
directory
php pool make:controller UserController
Let's take a look at an example of a basic controller. A controller may have any number of public methods which will respond to incoming HTTP requests:
<?php
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller
{
/**
* Show the profile for a given user.
*/
public function show(string $id)
{
return view('user.profile', [
'user' => User::find($id)
]);
}
}
Once you have written a controller class and method, you may define a route to the controller method like so:
use Phaseolies\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('/user/{id}', [UserController::class, 'show']);
Single Action Controllers
Doppar also support invokable controllers. You can call it single action controller also. To create a single action controller, need to pass the --i
option before create a controller.
php pool make:controller ProductController --i
This command will generate an invokable controller in the app\Http\Controllers
directory. The generated controller will look like this:
<?php
namespace App\Http\Controllers;
use Phaseolies\Http\Request;
use App\Http\Controllers\Controller;
class ProductController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
//
}
}
When using an invokable controller, the route definition looks like this:
Route::get('products', ProductController::class);
Invokable Controllers are also resolved via the service container, so you may type-hint any dependencies you need within a Invokable Controllers's constructor or directly in __invoke method.