Quanta Routing
Routing is a fundamental feature of any web application, and in Quanta, it provides a flexible and powerful way to map URLs to specific components or functions. Quanta's routing system is designed to be intuitive, highly customizable, and easy to extend, allowing you to define routes and their behavior in a way that fits your application's needs.
Routing Basics
In Quanta, routes are responsible for mapping URLs to specific logic. These routes can be simple URL patterns or more complex, dynamic patterns that accept parameters. Routes are handled by the Route Handler, which processes the incoming requests and matches them to the appropriate route.
Types of Routes in Quanta
Quanta supports several types of routes, each suited to different use cases. Below are the most common route types:
CleanRoute
The CleanRoute
class is the most common and basic route type in Quanta. It is used for simple static routes, where you define a specific URL or a list of URLs that should point to a particular component.
use Quanta\Core\CleanRoute;
// Define a clean route that maps multiple URLs to a specific componentId
$homeRoute = new CleanRoute("home", ["/", "/home", "/home/"], "home");
// Add the route to the route handler
$quanta->routeHandler->addRoute($homeRoute);
SimplePatternRoute
The SimplePatternRoute allows you to define routes with dynamic URL parameters, providing greater flexibility in handling URLs that contain variable segments (such as user IDs or slugs).
use Quanta\Core\SimplePatternRoute;
// Define a route with a dynamic parameter {user}
$testRoute = new SimplePatternRoute("test", "/test/{user}", function($quanta, $url, $matches) {
// Access the 'user' parameter from the URL
echo "User: " . $matches['user'];
});
// Add the route to the route handler
$quanta->routeHandler->addRoute($testRoute);
Preparing the Route with setPrepareCallback
One of the unique features of Quanta's routing system is the ability to prepare routes using the setPrepareCallback
method. This callback allows you to execute custom logic before the route is processed, enabling you to modify things like page metadata, environment variables, or other context-specific data.
How It Works
The setPrepareCallback
method is called right before a route is executed, allowing you to prepare any necessary data for that specific route.
Example: Adding Metadata
Here is an example of how you can use setPrepareCallback
to add metadata for SEO, Open Graph, and Twitter Cards:
use Quanta\Quanta;
use Quanta\Core\CleanRoute;
$homeRoute = new CleanRoute("home", ["/", "/home", "/home/"], "home");
// Set the prepare callback for the route
$homeRoute->setPrepareCallback(function (Quanta $quanta, string $url, mixed $extra) {
// Define the meta tags for SEO
$meta = [
"title" => "Quanta - Build Smarter",
"meta" => "Quanta is a lightweight and powerful PHP framework for building fast, scalable web applications.",
"keywords" => "PHP framework, Quanta, web development, fast PHP framework, modular architecture",
"author" => "Quanta Team",
"og_title" => "Quanta PHP Framework",
"og_description" => "Quanta is a lightweight PHP framework for modern web applications. Build fast, scalable solutions with a flexible architecture.",
"og_url" => "https://getquanta.dev",
"og_image" => "https://getquanta.dev/images/logo.svg",
"twitter_card" => "summary_large_image",
"twitter_title" => "Quanta PHP Framework",
"twitter_description" => "Build powerful web applications with Quanta, a fast and flexible PHP framework.",
"twitter_image" => "https://getquanta.dev/images/logo.svg",
"canonical" => "https://getquanta.dev" . $url
];
// Store the meta data in Quanta's memory
$quanta->memory->meta = $meta;
});
// Add the home route to the route handler
$quanta->routeHandler->addRoute($homeRoute);
In this example, we use the setPrepareCallback method to set SEO and social media metadata for the home page before it is rendered. The callback modifies Quanta's memory to store the metadata, which can be accessed and rendered in the corresponding template.
Parameters for setPrepareCallback
Quanta $quanta
: The Quanta instance, which provides access to global application settings and utilities.string $url
: The current URL being processed.mixed $extra
: Any additional data that may be passed to the route (such as parameters from the URL).
Routing Workflow
The routing process in Quanta consists of two main stages:
- Route Preparation: When
$quanta->prepareEnvironment()
is called, Quanta will check the URL for the route and execute its setPrepareCallback to set SEO metadata or other custom data. - Route Processing: The
$quanta->processRouting()
call will render the appropriate component based on the matched route
Conclusion
The routing system in Quanta is a powerful, flexible mechanism for mapping URLs to components or actions. With prepareEnvironment()
, you can prepare data specific to a route before it's rendered, and with processRouting()
, you can ensure the correct component is displayed based on the current URL. These two key functions work together to provide an organized and modular structure for web application development.