Controllers - Laravel Controllers
In Laravel, controllers are used to handle incoming HTTP requests and return an appropriate HTTP response. Controllers contain methods that define the application's behavior and respond to the user's input. In this article, we'll explore Laravel controllers in more detail.
Syntax
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController extends Controller
{
public function myMethod(Request $request)
{
// controller logic here
}
}
Example
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$users = User::all();
return view('users.index', ['users' => $users]);
}
public function create(Request $request)
{
return view('users.create');
}
public function store(Request $request)
{
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->save();
return redirect()->route('users.index')
->with('success', 'User created successfully.');
}
public function show(Request $request, $id)
{
$user = User::find($id);
return view('users.show', ['user' => $user]);
}
public function edit(Request $request, $id)
{
$user = User::find($id);
return view('users.edit', ['user' => $user]);
}
public function update(Request $request, $id)
{
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
if ($request->has('password')) {
$user->password = bcrypt($request->input('password'));
}
$user->save();
return redirect()->route('users.index')
->with('success', 'User updated successfully.');
}
public function destroy(Request $request, $id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users.index')
->with('success', 'User deleted successfully.');
}
}
Output
The controller methods return different types of outputs, depending on the application's requirements. In the example above, the index
method returns a view with the list of all users, the show
method returns a view with details of a specific user, and the store
, update
, and destroy
methods redirect back to the list of users with a success message.
Explanation
Controllers are the heart of any Laravel application. They receive the user's input through HTTP requests, handle the request, and return a response. In the example above, each method handles a specific HTTP request and returns an appropriate response. For example, the index
method returns a view with the list of all users, the create
method returns a view with a form to create a new user, and so on.
Use
Controllers are used to organize the application's logic and handle incoming HTTP requests. They allow you to separate the concerns of routing, request handling, and response handling. Controllers can also be used to interact with models and perform database operations.
Important Points
- Controllers are used to handle incoming HTTP requests and return an appropriate response.
- Controllers contain methods that define the application's behavior and respond to the user's input.
- Controllers can be used to organize the application's logic and perform database operations.
Summary
In this article, we explored Laravel controllers in detail. Controllers are the heart of any Laravel application, and they handle incoming HTTP requests and return an appropriate response. We also looked at a sample controller that handles CRUD operations for users, in order to give a practical use case for controllers.