Quanta PHP Framework - Example Setup
This example demonstrates how to set up the Quanta PHP Framework, add a basic route, and integrate a component. Quanta is a lightweight and powerful PHP framework designed for building fast, scalable web applications with a modular architecture.
1. Basic Setup
First, configure the environment and set up the basic framework structure.
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
use Quanta\Quanta;
// Instantiate Quanta
$quanta = new Quanta();
// Redirect 403 errors
$quanta->redirect403();
// Include necessary files
require("inc/components.php");
require("inc/actions.php");
require("inc/routes.php");
// Initialize the Action Handler
$quanta->actionHandler->init();
// Process the action
$quanta->processAction(true);
// Prepare the environment
$quanta->prepareEnvironment();
Explanation:
- Error Reporting: All errors are reported and displayed to the user using
error_reporting(E_ALL)
andini_set('display_errors', 1)
. - Autoloading: The
vendor/autoload.php
ensures that the necessary libraries are automatically loaded. - Quanta Instance: A new instance of the Quanta framework is created using
new Quanta()
. - Action Handler:
init()
initializes the action handler, andprocessAction(true)
processes the current action. - Environment Preparation:
prepareEnvironment()
prepares the application for further processing.
2. Example Route
Next, we create a route that responds to multiple URLs and sets up a callback to define meta data for the page.
$homeRoute = new CleanRoute("home", ["/", "/home", "/home/"], "home");
// Set a callback for preparing the route
$homeRoute->setPrepareCallback(function (Quanta $quanta, string $url, mixed $extra)
{
// Meta data for the page
$meta = [
"title" => "Quanta - Build Smarter",
"meta" => "Quanta is a lightweight and powerful PHP framework for building fast, scalable web applications with a modular and extensible architecture. Start now at getquanta.dev!",
"keywords" => "PHP framework, Quanta, web development, PHP, framework for developers, web applications, fast PHP framework, modular architecture, open-source PHP framework",
"author" => "Quanta Team",
"og_title" => "Quanta PHP Framework",
"og_description" => "Quanta is a lightweight PHP framework for modern web applications. Build fast and scalable solutions with a clear, 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. Perfect for developers who need modern solutions.",
"twitter_image" => "https://getquanta.dev/images/logo.svg",
"canonical" => "https://getquanta.dev" . $url
];
// Store meta data in Quanta's memory
$quanta->memory->meta = $meta;
});
// Add the route to the route handler
$quanta->routeHandler->addRoute($homeRoute);
Explanation:
- CleanRoute: A new route is created with multiple URL patterns ("/", "/home", "/home/") that are associated with the "home" route.
- PrepareCallback: A callback is set up to define meta data for the page, including title, description, SEO keywords, Open Graph, and Twitter Card data.
- Meta Data: These meta values are crucial for SEO and for how the page appears when shared on social media platforms.
- Route Handler: The route is then added to Quanta's route handler using
addRoute()
.
3. Example Component
This section shows how to create and register a component, in this case, a Navbar that renders a template.
class Navbar extends Component
{
// The render method loads the navbar template
public function render($quanta, $data)
{
$template = $quanta->loadTemplate("templates/navbar.php");
return $template;
}
}
// Add the Navbar component to the Quanta component handler
$quanta->componentHandler->addComponent(new Navbar("navbar"));
Explanation:
- Navbar Component: A new component is created by extending the
Component
class. Therender()
method loads thenavbar.php
template. - Component Registration: The Navbar component is added to the
componentHandler
so it can be used within the application.
Summary
- Setup and Configuration: The Quanta framework is set up with error reporting, configuration loading, and environment preparation.
- Routing: A route is created and a callback function is used to set meta data for SEO and social media sharing.
- Components: A Navbar component is created and added to the application to render a template.
This example demonstrates the modularity of Quanta, making it easy to add components and configure routes with minimal code.