Overview
The Application class is the heart of the MINI framework. It handles URL routing, controller instantiation, and method execution based on the request URL.
Located at: application/core/application.php
Class Definition
class Application
{
/** @var null The controller */
private $url_controller = null ;
/** @var null The method (of the above controller), often also named "action" */
private $url_action = null ;
/** @var array URL parameters */
private $url_params = array ();
public function __construct ()
{
// create array with URL parts in $url
$this -> splitUrl ();
// ... routing logic
}
}
Properties
The controller name extracted from the URL. This maps to a file in application/controller/.
The method name to call on the controller. Defaults to index() if not provided.
Additional URL segments passed as parameters to the controller method.
Constructor
__construct()
Initializes the application and processes the incoming request.
Process Flow:
Parse URL
Calls splitUrl() to extract controller, action, and parameters from the URL.
Load Controller
Checks if the controller file exists in application/controller/. If no controller is specified, loads the default Home controller.
Instantiate Controller
Creates an instance of the controller class.
Execute Method
Checks if the requested method exists and calls it with any URL parameters.
Handle Errors
Redirects to the error page (problem controller) if the controller or method doesn’t exist.
Example:
// In public/index.php
$app = new Application ();
// For URL: example.com/songs/editsong/17
// 1. Loads application/controller/songs.php
// 2. Instantiates Songs class
// 3. Calls $songs->editsong(17)
Private Methods
splitUrl()
Parses the URL and extracts routing information.
private function splitUrl ()
{
if ( isset ( $_GET [ 'url' ])) {
// split URL
$url = trim ( $_GET [ 'url' ], '/' );
$url = filter_var ( $url , FILTER_SANITIZE_URL );
$url = explode ( '/' , $url );
// Put URL parts into according properties
$this -> url_controller = isset ( $url [ 0 ]) ? $url [ 0 ] : null ;
$this -> url_action = isset ( $url [ 1 ]) ? $url [ 1 ] : null ;
// Remove controller and action from the split URL
unset ( $url [ 0 ], $url [ 1 ]);
// Rebase array keys and store the URL params
$this -> url_params = array_values ( $url );
}
}
URL Parsing Examples:
URL Controller Action Parameters /null → homenull → index[]/songssongsnull → index[]/songs/editsong/17songseditsong['17']/user/profile/john/settingsuserprofile['john', 'settings']
Routing Logic
The complete routing logic from the constructor:
if ( ! $this -> url_controller ) {
// No controller: load home page
require APP . 'controller/home.php' ;
$page = new Home ();
$page -> index ();
} elseif ( file_exists ( APP . 'controller/' . $this -> url_controller . '.php' )) {
// Controller exists: load and instantiate
require APP . 'controller/' . $this -> url_controller . '.php' ;
$this -> url_controller = new $this -> url_controller ();
if ( method_exists ( $this -> url_controller , $this -> url_action )) {
// Method exists: call it with parameters
if ( ! empty ( $this -> url_params )) {
call_user_func_array (
array ( $this -> url_controller , $this -> url_action ),
$this -> url_params
);
} else {
$this -> url_controller -> { $this -> url_action }();
}
} else {
if ( strlen ( $this -> url_action ) == 0 ) {
// No action: call default index() method
$this -> url_controller -> index ();
} else {
// Method doesn't exist: error page
header ( 'location: ' . URL . 'problem' );
}
}
} else {
// Controller doesn't exist: error page
header ( 'location: ' . URL . 'problem' );
}
Error Handling
When a controller file doesn’t exist, MINI redirects to /problem: header ( 'location: ' . URL . 'problem' );
This loads the Problem controller (formerly Error in PHP 5, renamed for PHP 7 compatibility).
When a controller exists but the method doesn’t:
If no action is specified, falls back to index()
If an action is specified but doesn’t exist, redirects to /problem
Usage Examples
Basic Routing
// URL: example.com/home
// Loads: application/controller/home.php
// Calls: Home::index()
public function index ()
{
require APP . 'view/_templates/header.php' ;
require APP . 'view/home/index.php' ;
require APP . 'view/_templates/footer.php' ;
}
Routing with Parameters
// URL: example.com/songs/editsong/17
// Loads: application/controller/songs.php
// Calls: Songs::editsong('17')
public function editsong ( $song_id )
{
$song = $this -> model -> getSong ( $song_id );
require APP . 'view/_templates/header.php' ;
require APP . 'view/songs/edit.php' ;
require APP . 'view/_templates/footer.php' ;
}
Multiple Parameters
// URL: example.com/blog/post/2024/03/hello-world
// Calls: Blog::post('2024', '03', 'hello-world')
public function post ( $year , $month , $slug )
{
$post = $this -> model -> getPostBySlug ( $year , $month , $slug );
// ... render view
}
Security Considerations
The URL is sanitized using filter_var($url, FILTER_SANITIZE_URL) before processing. However, you should still validate parameters in your controller methods.
All application logic outside the /public folder is protected by .htaccess, preventing direct access to controllers, models, and configuration files.
See Also
Controller Class Learn about the base Controller class
Routing Concept Deep dive into the routing system
Architecture Understand the request lifecycle
Controllers Guide How to create controllers