Overview
S-PHP implements the Model-View-Controller (MVC) architectural pattern to separate concerns and organize your application code. This pattern divides your application into three interconnected components:
Models : Handle data and business logic
Views : Manage the presentation layer
Controllers : Process requests and coordinate between Models and Views
Framework Structure
The S-PHP framework follows this directory structure:
├── Sphp/Core/ # Core framework classes
│ ├── Router.php # Routing system
│ ├── Controller.php # Base controller
│ ├── Models.php # Base model class
│ └── View.php # View rendering engine
├── app/
│ ├── Controllers/ # Your application controllers
│ ├── Models/ # Your application models
│ ├── views/ # Your view templates
│ ├── Middleware/ # Middleware classes
│ ├── router/ # Route definitions
│ └── config/ # Configuration files
└── public/
└── index.php # Application entry point
Controllers
Controllers handle incoming requests and return responses. All controllers extend the base Controller class which provides database access and configuration.
Base Controller
The base controller automatically initializes database connections:
namespace Sphp\Core ;
use Sphp\Core\ Database ;
class Controller
{
public $env ;
public $db ;
public function __construct ()
{
$this -> env = require ( '../app/config/config.php' );
$this -> db = new Database ( $this -> env );
}
}
Creating a Controller
Your controllers should extend the base Controller class:
app/Controllers/HomeController.php
namespace App\Controllers ;
use Sphp\Core\ Controller ;
use Sphp\Core\ View ;
class HomeController extends Controller
{
public function index ()
{
// Access database via $this->db
// Access config via $this->env
View :: render ( 'home.php' , [
'title' => 'Welcome'
]);
}
}
Controllers automatically have access to $this->db for database operations and $this->env for configuration values.
Models
Models represent your data layer and interact with the database. The base Models class provides CRUD operations.
Base Model Features
namespace Sphp\Core ;
class Models
{
protected $table ; // Database table name
protected $fillables = []; // Allowed fields for mass assignment
protected $hidden_fields ; // Fields to hide in responses
protected $db ; // Database connection
}
Creating a Model
Define your model by extending the base Models class:
namespace App\Models ;
use Sphp\Core\ Models ;
class User extends Models
{
protected $table = 'users' ;
protected $fillables = [
'name' ,
'email' ,
'password'
];
}
CRUD Operations
The base model provides these methods:
$user = new User ();
$user -> create ([
'name' => 'John Doe' ,
'email' => '[email protected] ' ,
'password' => password_hash ( 'secret' , PASSWORD_DEFAULT )
]);
Only fields listed in the $fillables array can be mass-assigned. This protects against mass assignment vulnerabilities.
Views
Views handle the presentation layer. The View class renders templates and supports layouts and components.
Rendering Views
use Sphp\Core\ View ;
// Simple view
View :: render ( 'home.php' );
// View with data
View :: render ( 'profile.php' , [
'user' => $user ,
'posts' => $posts
]);
View Engine Features
The View class supports:
Layouts : Reusable page structures
@ layout ( 'main' , [ 'title' => 'Home Page' ])
Components : Reusable UI elements
@ component ( 'navbar' , $navData )
Data Extraction : Variables are automatically extracted
View :: render ( 'page.php' , [ 'name' => 'John' ]);
// In view: <?php echo $name; ?>
View Directory Structure
app/views/
├── layout/ # Reusable layouts
├── components/ # Reusable components
├── home.php # Individual views
├── profile.php
└── 404.html # Error pages
MVC Flow
Here’s how a typical request flows through the MVC pattern:
Request arrives
The router receives the HTTP request and matches it to a route definition.
Controller processes
The router instantiates the controller and calls the specified method.
Model interaction
The controller uses models to fetch or manipulate data from the database.
View rendering
The controller passes data to a view for rendering the response.
Response sent
The view generates HTML which is sent back to the user.
Best Practices
Controllers should only handle request/response logic. Move business logic to models or service classes. // Good
public function store ()
{
$user = new User ();
$user -> create ( $_POST );
redirect ( '/users' );
}
// Avoid heavy logic in controllers
Always define $fillables in your models to prevent mass assignment vulnerabilities. protected $fillables = [ 'name' , 'email' ];
Keep PHP logic minimal in views. Prepare all data in the controller. // In controller
View :: render ( 'users.php' , [
'users' => $userModel -> select ([ '*' ]),
'count' => count ( $users )
]);
Next Steps
Routing Learn how to define routes and handle requests
Request Lifecycle Understand the complete request flow