The Routing
In the context of web development, a route is a mapping between a URL and an action. It defines the correspondence between a URL requested by the client and the code that will be executed to respond to that request.
A route typically consists of two main elements:
URL Path: This is the part of the URL that follows the application's domain name. For example, in the URL
https://mysite.com/articles
, the URL path is/articles
.HTTP Method: This is the method used by the client to send the request to the server. Commonly used HTTP methods are GET, POST, PUT, DELETE, etc. Each method represents a specific action to be performed on the resource associated with the route.
To define a route, you need to specify the URL path, the HTTP method, and the associated action. This is done directly in controller classes using attributes.
Here's an example of a route with PHP attributes in a controller:
<?php
namespace App\Controller;
use Qwwwest\Namaskar\Route;
use Qwwwest\Namaskar\Response;
class HomeController
{
#[Route('/', name: 'home', method:['GET'])]
public function index()
{
// Code for the index action
}
}
In this example, the #[Route(...)]
attribute is used to define the route. The URL path is /
, the route name is "home", and the HTTP method is GET.
Route Parameters
In the "Namaskar" framework, it's possible to define route parameters as PHP attributes in controllers.
<?php
namespace App\Controller;
use Qwwwest\Namaskar\Route;
use Qwwwest\Namaskar\Response;
use Qwwwest\Namaskar\AbstractController;
class MyController extends AbstractController
{
#[Route('/my-route/{id}', method:['GET'])]
public function myRoute(int $id): Response
{
// Use the value of the $id parameter in your processing logic
}
}
In this example, we've defined a method myRoute(int $id)
in the MyController
controller that will be called if a requested URL matches the route /my-route/{id}
, for example http://localhost/my-route/123
, where 123
is the ID you want to use.