Mada za sehemu hiiBuilding the back-end using a frameworkMada 3
- Implementing routing and handling of user requests
- Implementing interaction with databases
- Implementing authentication and authorisation
Implementing Routing and Handling of User Requests
When you visit a website and click a link or submit a form, the web server needs to know what to do with your request. In web frameworks, routes are the rules that connect a specific URL path (like /students or /about) to a function or controller that processes the request and returns a response. This process is called routing, and it is the foundation of any web application.
HTTP methods define the type of action being requested by the user. When you build a web application, you need to know what the user wants to do so your server can respond appropriately. Laravel supports several HTTP methods, but the four most common are:
- GET – Used to retrieve data from the server without making any changes. For example, viewing a list of students or reading a page.
- POST – Used to send new data to the server, usually when submitting a form such as registering a new student.
- PUT – Used to update existing data on the server, such as changing a student's details.
- DELETE – Used to remove data from the server, such as deleting a student record.
Each HTTP method has a specific purpose, and using the correct method makes your application secure and well-organized.
In Laravel, routes are defined in the routes/web.php file. You can create routes that use either closures (inline functions) or controllers.
Routes Using Closures
A closure is a function defined directly in the route file. Here is an example of defining a GET route:
Route::get('/students', function () {
return 'List of students';
});
This route listens for a GET request to /students and returns the text "List of students."
For POST requests (such as form submissions), you would write:
Route::post('/students', function () {
// Code to save a new student
return 'Student added';
});
The same pattern applies to PUT and DELETE routes.
Routes Using Controllers
A controller is a PHP class that organizes related route functions in one place. This keeps your route file clean and structured. Here is how you connect a route to a controller method:
Route::get('/students', [StudentController::class, 'index']);
This tells Laravel: "When someone visits /students using GET, run the index() method in the StudentController class."
Follow these steps to create routes for a student application:
Step 1: Create a Laravel Project
Open your terminal and run:
composer create-project laravel/laravel student-app
cd student-app
php artisan serve
Step 2: Create a Controller
Run this command to create a StudentController:
php artisan make:controller StudentController
Open app/Http/Controllers/StudentController.php and add these methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function index() {
return 'Displaying all students.';
}
public function create() {
return 'Add a new student.';
}
public function store(Request $request) {
return 'Student data received.';
}
public function delete($id) {
return "Student with ID $id deleted.";
}
}
Step 3: Define Routes
Open routes/web.php and add these routes:
use App\Http\Controllers\StudentController;
// GET route to display all students
Route::get('/students', [StudentController::class, 'index']);
// GET route to show the form for creating a new student
Route::get('/students/create', [StudentController::class, 'create']);
// POST route to save a new student
Route::post('/students', [StudentController::class, 'store']);
// DELETE route to remove a student
Route::delete('/students/{id}', [StudentController::class, 'delete']);
Notice how each route uses a different HTTP method. The {id} in the DELETE route is a parameter that allows the route to handle different student IDs dynamically.
After creating routes, you should test them to ensure they work correctly. Postman is a popular tool for testing APIs.
- Start your Laravel server:
php artisan serve - Open Postman
- For a GET request: Select GET, enter
http://127.0.0.1:8000/students, and click Send. You should see "Displaying all students." - For a POST request: Select POST, enter the same URL, go to the Body tab, select JSON, enter
{"name": "Amina", "class": "Form Three"}, and click Send. You should see "Student data received."
Testing helps you find and fix errors before users encounter them.
- Routes connect URL paths to functions or controllers that process user requests
- HTTP methods (GET, POST, PUT, DELETE) define the type of action
- Routes can use closures or controllers to handle requests
- Always test your routes using tools like Postman to ensure they work correctly
In Tanzania, schools can use routing to build a student results portal where teachers submit marks via POST routes while parents retrieve their children's results using GET routes. For example, a secondary school in Dar es Salaam could implement routes like /results/{student_id} to display exam scores, enabling secure and organized access to academic information.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Create and define routes for handling user requests.
- Test routes with sample requests and ensure correct response handling.
Tathmini ya matokeo
Functional server-side application that handles user requests and sends appropriate responses (e.g., JSON, HTML).
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how
- routing works in frameworks and the importance of RESTful principles.
Principles: The student should explain the principles of:
- clean URL structures, efficient routing, and error handling.
Theories: The student should explain:
- client-server architecture and HTTP protocol.
Circumstantial knowledge:
Detailed knowledge about:
- debugging routing errors and managing dynamic routes with parameters.
Swali
In a Laravel web application, what is the purpose of a route?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado