A lightweight express-like router for PHP with minimal setup required
- Static and dynamic routing with redirect support
- Send files, json data, or HTML output
- Set HTTP headers & status codes
<?php
require_once 'exPHPress.php';
$app = new exPHPress;
$app->get('/', function($req, $res) {
$res->sendFile('home.php');
});
$app->get('/profile/:id', function($req, $res) {
$res->sendFile('profile.php', [
'user_id' => $req['id']
]);
});
$app->get('/api/users/:id', function($req, $res) {
$user = getUser($req['id']);
$res->json($user);
});
Add the following settings to the document root block in your httpd.conf
file:
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
Note: Be sure to restart the server afterwards
Then add the following .htaccess
file to your app's directory:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|gif|svg)$
RewriteRule ^ app.php [L,QSA]
This will forward all non-resource requests to app.php
- Create an
app.php
file in your app directory - Download
exPHPress.php
and put it in the same directory - Add the following to
app.php
:
<?php
require_once 'exPHPress.php';
$app = new exPHPress;
And that's it!
$app->get('/', function() {
echo 'Hello world!';
});
$app->get()
defines a route using the http GET request method- You can also use
put()
,post()
,patch()
,delete()
, orany()
- You can also use
'/'
is the URL/pattern to be tested against the requested routefunction()
is the function to be called if the requested route matches'/'
$app->get('/greet/:name', function($req) {
$name = $req['name'];
echo "Hello $name!";
});
:name
is a parameter we want to retrieve from the requested route- Request parameters are defined by prepending them with a colon (
:
)
- Request parameters are defined by prepending them with a colon (
$req
is an associative array containing the request parameters and their values- The value of a request parameter can be accessed by referencing it in
$req
, such as$req['name']
- The value of a request parameter can be accessed by referencing it in
$app->get('/profile', function($req, $res) {
$res->sendFile('profile.php');
});
$res
is the response object, containing some useful built-in functions$res->sendFile()
is the response function for sending a file to the client- Accepts either a PHP or HTML file
$app->get('/profile/:id', function($req, $res) {
$res->sendFile('profile.php', [
'user_id' => $req['id']
]);
});
$res->sendFile()
accepts an optional second parameter - an associative array of variables to be extracted to the file being sent- In
profile.php
, the variable$user_id
would be equal to the request parameter:id
, because it is passed via$req['id']
- In
$app->static('public/views');
$app->static()
defines a default directory for$res->sendFile()
to send files from- File paths starting with
/
,./
, or../
will ignore the static directory
- File paths starting with
public/views
is the new directory in which$res->sendFile()
will look for files
$app->get('/api/users/123', function($req, $res) {
$res->json([
'name' => 'John',
'age' => 35
], 200);
});
$res->json()
is the response function for sending json data to the client- Accepts either a PHP associative array or a valid json string
200
is an optional second parameter for sending an HTTP status code along with the json data
$app->get('/', function($req, $res) {
$res->sendStatus(201);
});
$res->sendStatus()
is the response function for sending an http status code- This route will respond with
http 201
- This route will respond with
$app->get('/', function($req, $res) {
$res->setHeader('Content-Type', 'application/json');
});
$res->setHeader()
is the response function for setting http headers- Accepts either
(key, value)
or an associative array of multiple keys and values
- Accepts either
$app->get('/profile', function($req, $res) {
$res->redirect('/profile/nifte');
});
$res->redirect()
is the response function for redirecting one route to another- The redirected route will keep the same http request method
$app->error(function($req, $res) {
$res->sendStatus(404);
$res->sendFile('404.php');
});
$app->error()
defines the function to be run when the requested route does not match any defined routes- If the
$app->error()
function is not defined, invalid routes will simply returnhttp 404
with the message 'Page not found.'
- If the