carlosroberto555/expressphp

Biblioteca para roteamento (Similar a ExpressJS)

v1.0.10 2021-11-06 20:01 UTC

README

A express like lib for PHP language

Installation

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

Quick Start

Basic use;

<?php
include 'vendor/autoload.php';

$app = new ExpressPHP\Express;

$app->use('/', function ($req, $res) {
  $res->send('Hello world!');
});

Include a children app scope

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'],
  ]);
});

Include a static content children route

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>