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:

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:

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:

Summary

This example demonstrates the modularity of Quanta, making it easy to add components and configure routes with minimal code.