Defining Routes¶
Routing is how you tell PhpSPA which component to show for a specific URL. This is done with the ->route() method.
Route Assignment
You create a component for each page of your site and assign it a unique URL path.
Route Syntax¶
The route() method maps a URL path to a component. Use / for the home page, /about for an about page, etc.
Example¶
Simple Website
Let's create a simple website with a home page and an about page.
1. Setup App
Load your layout and create the app instance.
2. Create Home Page
<?php
$homePage = new Component(function () {
echo '<h1>Welcome to the Home Page!</h1>';
});
$homePage->route('/');
Define the home page component and map it to /.
3. Create About Page
<?php
$aboutPage = new Component(function () {
echo '<h1>About Our Company</h1>';
});
$aboutPage->route('/about');
Define the about page component and map it to /about.
4. Attach and Run
Register components and render the app.
Complete Example¶
<?php
use PhpSPA\App;
use PhpSPA\Component;
// Assume our layout is loaded
$layout = require __DIR__ . '/layout.php';
$app = new App($layout);
// --- Create the Home Page Component ---
$homePage = new Component(function () {
echo '<h1>Welcome to the Home Page!</h1>';
});
$homePage->route('/'); // Maps this component to the root URL
// --- Create the About Page Component ---
$aboutPage = new Component(function () {
echo '<h1>About Our Company</h1>';
});
$aboutPage->route('/about'); // Maps this component to the /about URL
// Attach both components to the app
$app->attach($homePage);
$app->attach($aboutPage);
// The router will now render the correct component
$app->run();
That's it
When a user visits your site at /about, PhpSPA will automatically render the $aboutPage component.