Skip to content
PHP micro-framework inspired by Express.js
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
docs
examples
src
tests
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml

README.md

php-express

Build Status Latest Stable Version License

PHP micro-framework inspired by Express.js

Requirements

  • PHP >= 7.0

Installation

If Composer is not installed on your system yet, you may go ahead and install it using this command line:

$ curl -sS https://getcomposer.org/installer | php

Next, add the following require entry to the composer.json file in the root of your project.

{
    "require" : {
        "riverside/php-express" : "*"
    }
}

Finally, use Composer to install php-express and its dependencies:

$ php composer.phar install 

Routing

<?php
$app = new \PhpExpress\Application();

$app->get('/', function ($req, $res) {
     $res->send('hello world');
});

Route methods

<?php
// GET method route
$app->get('/', function ($req, $res) {
    $res->send('GET request to the homepage');
});

// POST method route
$app->post('/', function ($req, $res) {
    $res->send('POST request to the homepage');
});

Route paths

<?php
$app->get('/', function ($req, $res) {
    $res->send('root');
});

$app->get('/about', function ($req, $res) {
    $res->send('about');
});

$app->get('/random.text', function ($req, $res) {
    $res->send('random.text');
});

Response methods

Method Description
$res->end() End the response process.
$res->json() Send a JSON response.
$res->redirect() Redirect a request.
$res->render() Render a view template.
$res->send() Send a response of various types.
$res->sendStatus() Set the response status code and send its string representation as the response body.

$app->route()

<?php
$app->route('/book')
    ->get(function ($req, $res) {
        $res->send('Get a random book');
    })
    ->post(function ($req, $res) {
        $res->send('Add a book');
    })
    ->put(function ($req, $res) {
        $res->send('Update the book');
    });

PhpExpress Router

<?php
$router = new \PhpExpress\Router($app);

$router->get('/', function ($req, $res) {
    $res->send('Birds home page');
});

$router->get('/about', function ($req, $res) {
    $res->send('About birds');
});

$router->run();
You can’t perform that action at this time.