Categories

PHP micro-framework

Posted on: May 3, 2020 by Dimitar Ivanov

php-express is a minimal and flexible PHP web application framework that provides a robust set of features for web and mobile applications. php-express is a heavily inspired by Express.js

php-express
Figure 1. php-express micro framework
Table of contents

Requirements

  • PHP >= 7.0
  • JSON extension

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

Configuration

Make sure that all the requests goes through a single php file.

  • Apache
    <IfModule mod_rewrite.c>
        RewriteEngine On  
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?_path_=$1 [QSA,L]
    </IfModule>
    
  • Nginx
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

API

  • Application - The $app object conventionally denotes the php-express application.
    $app = new \PhpExpress\Application();
    ...
    $app->run();
    
  • Request - The $req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
    $app->get('/', function ($req, $res) {
        $res->send('HTTP method is: ' . $req->method);
    });
    
  • Response - The $res object represents the HTTP response that a php-express app sends when it gets an HTTP request.
    $app->get('/readme.txt', function ($req, $res) {
        $res->set('Content-Type', 'text/plain');
        $res->send('foo bar');
    });
    
  • Router - A $router object is an isolated instance of middleware and routes. You can think of it as a "mini-application", capable only of performing middleware and routing functions. Every php-express application has a built-in app router.
    $router = new \PhpExpress\Router();
    
    $router->get('/', function ($req, $res) {
        $res->send('foo bar');
    });
    
    $router->run();
    

Controllers

php-express support the concepts of Controller and View from the MVC architectural pattern. To employ the Controller concept we need to:

  • specify the class name and method name that will handle the request
  • define the above class and method
$app->get('about.html', '\controllers\basic@about');

This is an example of class basic.

namespace controllers;

class basic {
    public function about($req, $res) {
        $res->send('foo bar');
    }
}

Views

To employ the View concept in php-express we need to:

  • configure the path to templates using the set() method
  • specify the name of desired template using the render() method
$app->set("views", __DIR__ . "/views");
$app->get('about.html', function ($req, $res) {
    $res->render('about');
});

Downloads

The project is hosted on GitHub.

License

php-express is licensed under the MIT license.

See also
Share this post

If you like the php-express please share this link with your friends and colleagues.


0 Comments

Comments are closed