Skip to main content
Make sure you’ve completed the installation before proceeding with this guide.

Understanding the request flow

When a user visits your S-PHP application, the request flows through several components:
  1. Entry Point (public/index.php) - Handles autoloading and routes to web/API handlers
  2. Router - Matches the URL to a route definition
  3. Middleware - Checks permissions and authentication (optional)
  4. Controller - Processes the request and prepares data
  5. View - Renders the response to the user

Your first route

Routes are defined in app/router/web.php. Here’s a simple example from the source:
app/router/web.php
<?php

use App\Controllers\HomeController;
use Sphp\Core\Router;

$router = new Router();

// Define a GET route
$router->get('/', HomeController::class, 'index');

$router->dispatch();

Your first controller

Controllers live in app/Controllers/. Here’s a basic controller:
app/Controllers/HomeController.php
<?php

namespace App\Controllers;

use Sphp\Core\Controller;
use Sphp\Core\View;

class HomeController extends Controller
{
    public function index()
    {
        return View::render('welcome.php', [
            'title' => 'Welcome to S-PHP',
            'version' => '2.0'
        ]);
    }
}

Dynamic routes

S-PHP supports dynamic parameters in routes using {parameter} syntax:
$router->get('/user/{id}', HomeController::class, 'showUser');
The controller method receives the parameter as an argument:
public function showUser($id)
{
    // $id contains the value from the URL
    $user = $this->db->query("SELECT * FROM users WHERE id = ?", [$id]);
    
    return View::render('user.php', ['user' => $user]);
}

Building a simple page

Let’s build a complete feature from scratch.
1

Create a route

Add a new route in app/router/web.php:
$router->get('/about', HomeController::class, 'about');
2

Add controller method

Add the about method to HomeController:
public function about()
{
    return View::render('about.php', [
        'title' => 'About Us',
        'description' => 'Learn more about our framework'
    ]);
}
3

Create the view

Create app/views/about.php:
@layout('main', ['title' => $title])

<div class="container">
    <h1><?= $title ?></h1>
    <p><?= $description ?></p>
</div>
4

Test it

Start the development server:
php do up
Visit http://localhost:8000/about in your browser.

Working with databases

S-PHP provides a simple database interface through the controller’s $db property:
public function users()
{
    // Query the database
    $users = $this->db->query("SELECT * FROM users WHERE active = ?", [1]);
    
    return View::render('users.php', ['users' => $users]);
}

Using middleware

Protect routes with middleware by adding it as the 4th parameter:
use App\Middleware\VerifiedUser;

$router->get('/dashboard', HomeController::class, 'dashboard', VerifiedUser::class);
The middleware checks if the user is authenticated before allowing access:
app/Middleware/VerifiedUser.php
<?php

namespace App\Middleware;

use Sphp\Services\Auth;

class VerifiedUser extends Middleware
{
    public function handle()
    {
        if (Auth::check()) {
            return true; // Allow access
        }
        
        redirect('/login'); // Redirect to login
    }
}

Handling POST requests

Add a POST route for form submissions:
$router->post('/contact', HomeController::class, 'submitContact');
Access form data in the controller:
use Sphp\Core\Request;

public function submitContact()
{
    $data = Request::request();
    
    $name = $data['name'];
    $email = $data['email'];
    $message = $data['message'];
    
    // Process the form data
    $this->db->query(
        "INSERT INTO contacts (name, email, message) VALUES (?, ?, ?)",
        [$name, $email, $message]
    );
    
    redirect('/thank-you');
}

CLI commands

S-PHP includes a powerful CLI tool for common tasks:
# Start development server
php do up

# Create a new controller
php do controller UserController

# Create a new model
php do model User

# Create a new view
php do views user/profile

# Run database migrations
php do migrate

Next steps

Now that you’ve built your first route, explore more features:

Controllers

Learn about controllers and request handling

Models

Work with the database using models

Views

Create dynamic templates with layouts

Authentication

Add user authentication with JWT

Build docs developers (and LLMs) love