Views
in package
Class to handle routes using views.
Router class for handling view-based routes with layout support. Manages routing for traditional server-rendered pages, supporting dynamic parameters, layouts, view rendering, and 404 error handling. Provides utilities for URL generation and redirects.
Table of Contents
Properties
- $defaultLayout : string
- Default layout to use for views
- $layoutsPath : string
- Path to the layouts directory
- $notFoundView : string
- View to display for 404 errors
- $routes : array<string|int, mixed>
- Collection of registered view routes
- $viewsPath : string
- Path to the views directory
Methods
- __construct() : mixed
- Constructor
- get() : void
- Registers a GET route for a view
- param() : mixed|null
- Retrieves a request parameter from GET or POST data
- post() : void
- Registers a POST route for a view
- redirect() : void
- Redirects to a specified URL
- render() : void
- Renders a view with optional layout
- run() : void
- Dispatches the current request to the matching view route
- setDefaultLayout() : void
- Sets the default layout for all views
- setNotFoundView() : void
- Sets the view to display for 404 errors
- url() : string
- Generates a URL by replacing route parameters with actual values
- addRoute() : void
- Adds a route to the routes collection
Properties
$defaultLayout
Default layout to use for views
private
string
$defaultLayout
= 'main'
$layoutsPath
Path to the layouts directory
private
string
$layoutsPath
$notFoundView
View to display for 404 errors
private
string
$notFoundView
= '404'
$routes
Collection of registered view routes
private
array<string|int, mixed>
$routes
= []
$viewsPath
Path to the views directory
private
string
$viewsPath
Methods
__construct()
Constructor
public
__construct(string $viewsPath, string $layoutsPath) : mixed
Initializes the view router with paths to views and layouts directories.
Parameters
- $viewsPath : string
-
Path to the views directory
- $layoutsPath : string
-
Path to the layouts directory
get()
Registers a GET route for a view
public
get(string $path, string $view[, array<string|int, mixed> $data = [] ][, string|null $layout = null ]) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $view : string
-
View name (without .php extension)
- $data : array<string|int, mixed> = []
-
Data to pass to the view
- $layout : string|null = null
-
Layout name to use (null uses default layout)
param()
Retrieves a request parameter from GET or POST data
public
static param(string $param) : mixed|null
Static utility method for accessing request parameters.
Parameters
- $param : string
-
Parameter name
Return values
mixed|null —Parameter value or null if not found
post()
Registers a POST route for a view
public
post(string $path, string $view[, array<string|int, mixed> $data = [] ][, string|null $layout = null ]) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $view : string
-
View name (without .php extension)
- $data : array<string|int, mixed> = []
-
Data to pass to the view
- $layout : string|null = null
-
Layout name to use (null uses default layout)
redirect()
Redirects to a specified URL
public
redirect(string $url[, int $statusCode = 302 ]) : void
Sets the HTTP status code and Location header, then terminates execution.
Parameters
- $url : string
-
Target URL for redirection
- $statusCode : int = 302
-
HTTP status code (default: 302 for temporary redirect)
Return values
void —Sends redirect header and exits
render()
Renders a view with optional layout
public
render(string $view[, array<string|int, mixed> $data = [] ][, string|null $layout = null ]) : void
Loads the view file, extracts data as variables, captures the output, and wraps it in a layout if specified. The layout file should include the $content variable to display the view content.
Parameters
- $view : string
-
View name (without .php extension)
- $data : array<string|int, mixed> = []
-
Associative array of data to pass to the view
- $layout : string|null = null
-
Layout name (null uses default, 'none' for no layout)
Tags
Return values
void —Outputs the rendered view
run()
Dispatches the current request to the matching view route
public
run() : void
Matches the current HTTP method and URI against registered routes, extracts dynamic parameters, merges them with route data, and renders the corresponding view. Displays 404 view if no matching route is found.
Return values
void —Renders view or 404 page
setDefaultLayout()
Sets the default layout for all views
public
setDefaultLayout(string $layout) : void
Parameters
- $layout : string
-
Layout name (without .php extension)
setNotFoundView()
Sets the view to display for 404 errors
public
setNotFoundView(string $view) : void
Parameters
- $view : string
-
View name (without .php extension)
url()
Generates a URL by replacing route parameters with actual values
public
url(string $path[, array<string|int, mixed> $params = [] ]) : string
Useful for creating URLs dynamically based on route definitions.
Parameters
- $path : string
-
Route path template with {param} placeholders
- $params : array<string|int, mixed> = []
-
Associative array of parameter names and values
Return values
string —Generated URL with parameters replaced
addRoute()
Adds a route to the routes collection
private
addRoute(string $method, string $path, string $view, array<string|int, mixed> $data, string|null $layout) : void
Converts route path with {param} syntax to a regular expression pattern for matching and parameter extraction.
Parameters
- $method : string
-
HTTP method (GET, POST)
- $path : string
-
Route path with optional dynamic parameters
- $view : string
-
View name
- $data : array<string|int, mixed>
-
Data to pass to the view
- $layout : string|null
-
Layout name