Skip to main content

Overview

Controllers handle incoming HTTP requests and return responses to the user. In S-PHP, controllers extend the base Controller class and have automatic access to the database and environment configuration.

Creating a Controller

Controllers are located in the app/Controllers directory and extend the Sphp\Core\Controller class.
app/Controllers/HomeController.php
<?php

namespace App\Controllers;

use Sphp\Core\Controller;
use Sphp\Core\View;
use App\Models\Users;

class HomeController extends Controller
{
    protected $user;
    
    public function __construct()
    {
        parent::__construct();
        $this->user = new Users();
    }
    
    public function index()
    {
        View::render('welcome.php');
    }
}

Base Controller Class

The Controller class provides two essential properties that are automatically available in all controllers:
$env
array
Environment configuration loaded from app/config/config.php
$db
Database
Database instance for executing queries

Constructor Behavior

When you instantiate a controller, the constructor automatically:
  1. Loads the environment configuration from app/config/config.php
  2. Creates a new Database instance
Sphp/Core/Controller.php
public function __construct()
{
    $this->env = require('../app/config/config.php');
    $this->db = new Database($this->env);
}

Accessing the Database

You can access the database directly through the $this->db property:
class UserController extends Controller
{
    public function getUsers()
    {
        $query = "SELECT * FROM users WHERE active = ?";
        $users = $this->db->query($query, [1]);
        
        return $users;
    }
}

Controller Methods

Controller methods represent different actions or endpoints in your application:
app/Controllers/HomeController.php
class HomeController extends Controller
{
    public function index()
    {
        View::render('welcome.php');
    }
    
    public function register()
    {
        try {
            $request = Request::request();
            $this->user->save($request);
        } catch (Exception $e) {
            // Handle error
        }
    }
    
    public function login()
    {
        try {
            $request = Request::request();
            $bearer_token = Auth::login($request);
            
            return $this->successResponse("Login success", $bearer_token);
        } catch (Exception $e) {
            return $this->errorResponse("Error try again");
        }
    }
}

Using Models in Controllers

Controllers commonly work with models to interact with the database:
class HomeController extends Controller
{
    protected $user;
    
    public function __construct()
    {
        parent::__construct();
        $this->user = new Users();
    }
    
    public function index()
    {
        $users = $this->user->select(['*']);
        View::render('users/index.php', ['users' => $users]);
    }
}

Rendering Views

Use the View::render() method to display views from your controller:
public function index()
{
    View::render('welcome.php');
}

// With data
public function show($id)
{
    $user = $this->user->findByID($id);
    View::render('users/show.php', ['user' => $user]);
}

Best Practices

Keep Controllers Thin

Move business logic to models and services. Controllers should only handle HTTP concerns.

Use Dependency Injection

Initialize models and services in the constructor for cleaner code.

Handle Exceptions

Always wrap potentially failing operations in try-catch blocks.

Return Consistent Responses

Use consistent response formats for APIs (successResponse, errorResponse).

Next Steps

Models

Learn how to work with models and database operations

Views

Discover how to render views and use templates

Build docs developers (and LLMs) love