A express like lib for PHP language
You need first to have composer previous installed. You can see more on https://getcomposer.org.
Now you install this package using:
$ composer require carlosroberto555/expressphp
Basic use;
<?php
include 'vendor/autoload.php';
$app = new ExpressPHP\Express;
$app->use('/', function ($req, $res) {
$res->send('Hello world!');
});
The new app that includes a children route. The Express app has a static method require, to include a php executable file with actual route scope.
<?php
include 'vendor/autoload.php';
$app = new ExpressPHP\Express;
$app->use('/api', app::require('/routes/api.php'));
Children route:
<?php
// file /routes/api.php
$router = ExpressPHP\Express::Router();
// GET /api/users
$router->get('/users', function ($req, $res) {
$res->json([
['name' => 'Libbie Dunn'],
['name' => 'Ella-Mai Davies'],
['name' => 'Elsie-Rose Dennis'],
['name' => 'Zena Slater'],
['name' => 'Antoni Partridge'],
]);
});
Express has a method called static to include static content. This just send files like images, html, css, js with a cache control header I-Modiffied-Since
.
<?php
include 'vendor/autoload.php';
use ExpressPHP\Express as app;
$app = new app;
$app->use('/hello', app::static('/static/index.html'));
// $app->use('/css', app::static('/static/css'));
// $app->use('/uploads', app::static('/static/images/uploads'));
The html example file:
<!DOCTYPE html>
<!-- /static/index.html -->
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h1>Hello world</h1>
</body>
</html>