Controllers help organize your code by handling user requests and sending responses. Methods inside controllers define what happens for each action.
When you want to handle a form submission and save data.
When you need to show a list of items from the database.
When you want to update or delete a resource based on user input.
When you want to separate your app logic from routes for cleaner code.
Syntax
Laravel
class ExampleController extends Controller {
public function actionName() {
// code to handle the action
}
}
Each method inside a controller is called an action.
Actions usually return a view, redirect, or JSON response.
Examples
This method shows a list of users by returning a view.
Laravel
class UserController extends Controller {
public function index() {
return view('users.index');
}
}
This method handles saving a new post from form data.
Laravel
class PostController extends Controller {
public function store(Request $request) {
// save new post
}
}
This method deletes a product using its ID.
Laravel
class ProductController extends Controller {
public function destroy($id) {
// delete product by id
}
}
Sample Program
This controller has two methods: one returns a simple greeting, the other greets a user by name.
Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class GreetingController extends Controller
{
public function hello() {
return'Hello, welcome to Laravel!';
}
public function greetUser($name) {
return"Hello, {$name}! Nice to see you.";
}
}
OutputSuccess
Important Notes
Controller methods should be kept simple and focused on one task.
Use dependency injection to get request data or services inside methods.
Always return a response from controller methods, like a view or string.
Summary
Controllers group related actions to handle requests.
Each method inside a controller is an action that does something.
Actions return responses like views, redirects, or strings.
Practice
(1/5)
1. What is the main purpose of a controller method (action) in Laravel?
easy
A. To handle a specific user request and return a response
B. To define database schema
C. To style the HTML output
D. To manage server configuration
Solution
Step 1: Understand controller role
Controllers group related logic into methods called actions.
Step 2: Identify action purpose
Each action handles a user request and returns a response like a view or JSON.
Final Answer:
To handle a specific user request and return a response -> Option A
Quick Check:
Controller action = handle request and respond [OK]
Hint: Controllers handle requests and responses, not styling or config [OK]
Common Mistakes:
Confusing controller methods with database or styling tasks
Thinking controllers manage server settings
Assuming controllers only return views
2. Which of the following is the correct syntax to define a controller method named show in Laravel?
easy
A. function show() public { /* code */ }
B. public function show() { /* code */ }
C. public show function() { /* code */ }
D. def show() { /* code */ }
Solution
Step 1: Recall PHP method syntax
In PHP, methods are defined with visibility, function keyword, name, and parentheses.
Step 2: Match Laravel controller method syntax
Laravel uses PHP syntax: public function methodName() {}.