Veronica  Roob

Veronica Roob


Awesome PHP: Micro Frameworks and Routers

Micro Frameworks

Micro frameworks and routers.

  • Laravel-Zero - A micro-framework for console applications.
  • Lumen - A micro-framework by Laravel.
  • Mezzio - A micro-framework by Laminas.
  • Radar - An Action-Domain-Responder implementation for PHP.
  • Silly - A micro-framework for CLI applications.
  • Slim - Another simple micro framework.

Author: ziadoz
Source Code:
License: WTFPL License

#php #frameworks 

What is GEEK

Buddha Community

Awesome PHP: Micro Frameworks and Routers

Hire PHP Developer - Best PHP Web Frameworks for Web Development

A framework that can drastically cut down the requirement to write original code to develop the web apps as per your requirement is PHP Framework. PHP frameworks offer code libraries for commonly used functions to reduce the development time.

Want to use PHP Web Frameworks for your web applications?

WebClues Infotech offers a service to hire dedicated PHP developers for all of the below-mentioned frameworks

  • Laravel Developer
  • Codeigniter Developer
  • Yii Developer
  • Zend Developer
  • Cake PHP Developer
  • Core PHP Developer

Not sure which framework to use for your PHP web application?

Contact us

Schedule Interview with PHP Developer


#hire php developer #hire php web developers #hire php developer in 2021 #hire php developers & dedicated php programmers #hire php developers india #hire and outsource freelance php developers

Top 3 PHP Libraries for Extras Related To Micro Frameworks & Routers

In today's post we will learn about top 3 PHP Libraries for Extras Related To Micro Frameworks & Routers. 

What is Micro Frameworks?

A microframework is a term used to refer to minimalistic web application frameworks. It is contrasted with full-stack frameworks.

It lacks most of the functionality which is common to expect in a full-fledged web application framework, such as:

  • Accounts, authentication, authorization, roles
  • Database abstraction via an object-relational mapping
  • Input validation and input sanitation
  • Web template engine

Typically, a microframework facilitates receiving an HTTP request, routing the HTTP request to the appropriate controller, dispatching the controller, and returning an HTTP response. Microframeworks are often specifically designed for building the APIs for another service or application. For example, Lumen microframework is designed for Microservices development and API development.

Table of contents:

  • Slim Skeleton - A skeleton for Slim.
  • Slim Twig View - Integrate Twig into Slim.
  • Slim PHP View - A simple PHP renderer for Slim.

1 - Slim Skeleton: 

A skeleton for Slim.

Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This application uses the latest Slim 4 with Slim PSR-7 implementation and PHP-DI container implementation. It also uses the Monolog logger.

This skeleton application was built for Composer. This makes setting up a new Slim Framework application quick and easy.

Install the Application

Run this command from the directory in which you want to install your new Slim Framework application. You will require PHP 7.4 or newer.

composer create-project slim/slim-skeleton [my-app-name]

Replace [my-app-name] with the desired directory name for your new application. You'll want to:

  • Point your virtual host document root to your new application's public/ directory.
  • Ensure logs/ is web writable.

To run the application in development, you can run these commands

cd [my-app-name]
composer start

Or you can use docker-compose to run the app with docker, so you can run these commands:

cd [my-app-name]
docker-compose up -d

After that, open http://localhost:8080 in your browser.

Run this command in the application directory to run the test suite

composer test

That's it! Now go build something cool.

View on Github

2 - Slim Twig View: 

Integrate Twig into Slim.

This repository contains custom View classes for the template frameworks listed below. You can use any of these custom View classes by either requiring the appropriate class in your Slim Framework bootstrap file and initialize your Slim application using an instance of the selected View class or using Composer (the recommended way).

Slim Views only officially support the following views listed below.

  • Smarty
  • Twig

How to Install

using Composer

Install in your project by running the following composer command:

$ php composer require slim/views


How to use

require 'vendor/autoload.php';

$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Smarty()

To use Smarty options do the following:

$view = $app->view();
$view->parserDirectory = dirname(__FILE__) . 'smarty';
$view->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$view->parserCacheDirectory = dirname(__FILE__) . '/cache';


How to use

require 'vendor/autoload.php';

$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Twig()

To use Twig options do the following:

$view = $app->view();
$view->parserOptions = array(
    'debug' => true,
    'cache' => dirname(__FILE__) . '/cache'

In addition to all of this we also have a few helper functions which are included for both view parsers. In order to start using these you can add them to their respective view parser as stated below:


$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),


$view->parserExtensions = array(
    dirname(__FILE__) . '/vendor/slim/views/Slim/Views/SmartyPlugins',

View on Github

3 - Slim PHP View:

A simple PHP renderer for Slim.

This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4.

Cross-site scripting (XSS) risks

Note that PHP-View has no built-in mitigation from XSS attacks. It is the developer's responsibility to use htmlspecialchars() or a component like laminas-escaper. Alternatively, consider Twig-View.


Install with Composer:

composer require slim/php-view

Usage with Slim 4

use Slim\Views\PhpRenderer;

include "vendor/autoload.php";

$app = Slim\AppFactory::create();

$app->get('/hello/{name}', function ($request, $response, $args) {
    $renderer = new PhpRenderer('path/to/templates');
    return $renderer->render($response, "hello.php", $args);


Note that you could place the PhpRenderer instantiation within your DI Container.

Usage with any PSR-7 Project

//Construct the View
$phpView = new PhpRenderer("path/to/templates");

//Render a Template
$response = $phpView->render(new Response(), "hello.php", $yourData);

View on Github

Thank you for following this article.

Related videos:

Is this the best PHP Routing library?

#php #micro #framework 

Mike  Kozey

Mike Kozey


10 of the Best PHP Testing Frameworks for 2021

We will discuss how the best PHP testing frameworks approach test automation and what pros or cons they all offer. Let’s look at the best PHP frameworks.

A framework is a collection or set of tools and processes that work together to support testing and developmental activities. It contains various utility libraries, reusable modules, test data setup, and other dependencies. Be it web development or testing, there are multiple frameworks that can enhance your team’s efficiency and productivity. Web testing, in particular, has a plethora of frameworks, and selecting a framework that suits your needs depends on your language of choice.

Amongst all server-side programming languages,  80% of websites use PHP, and the right framework can make the job easier. We decided to dive deeper into PHP and find out what the best PHP testing frameworks are. In this blog, we will be focusing on automated testing frameworks and will be listing out the best PHP frameworks that will allow you to write your test cases in a standard format.

Best PHP Testing Frameworks of 2021

Several frameworks have been and are being used for development and testing purposes. But each one is unique in its own way and offers unique features. We will discuss how the best PHP testing frameworks approach test automation and what pros or cons they all offer. Without further ado, let’s look at the best PHP frameworks.

  1. PHPUnit
  2. Codeception
  3. Storyplayer
  4. SeleniumHQ
  5. Behat
  6. Atoum
  7. SimpleTest
  8. phpSpec
  9. Peridot
  10. Kahlan

#php #php framework #php frameworks

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? has always been an industry leader for companies and business owners looking to hire PHP web developer. By choosing to Hire PHP Developer from our company, you can always expect the best results. Our PHP services and solutions are always flexible which means that no matter the nature of your project, you can always count on us for getting the best PHP expertise.

Consult with our experts:

#hire php developer #php developer #php development company #php development services #php development #php

Flight: An Extensible Micro-framework for PHP

What is Flight?

Flight is a fast, simple, extensible framework for PHP. Flight enables you to quickly and easily build RESTful web applications.

require 'flight/Flight.php';

Flight::route('/', function(){
    echo 'hello world!';


Learn more


Flight requires PHP 7.4 or greater.


Flight is released under the MIT license.


1. Download the files.

If you're using Composer, you can run the following command:

composer require mikecao/flight

OR you can download them directly and extract them to your web directory.

2. Configure your webserver.

For Apache, edit your .htaccess file with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

Note: If you need to use flight in a subdirectory add the line RewriteBase /subdir/ just after RewriteEngine On.

For Nginx, add the following to your server declaration:

server {
    location / {
        try_files $uri $uri/ /index.php;

3. Create your index.php file.

First include the framework.

require 'flight/Flight.php';

If you're using Composer, run the autoloader instead.

require 'vendor/autoload.php';

Then define a route and assign a function to handle the request.

Flight::route('/', function(){
    echo 'hello world!';

Finally, start the framework.



Routing in Flight is done by matching a URL pattern with a callback function.

Flight::route('/', function(){
    echo 'hello world!';

The callback can be any object that is callable. So you can use a regular function:

function hello(){
    echo 'hello world!';

Flight::route('/', 'hello');

Or a class method:

class Greeting {
    public static function hello() {
        echo 'hello world!';

Flight::route('/', array('Greeting', 'hello'));

Or an object method:

class Greeting
    public function __construct() {
        $this->name = 'John Doe';

    public function hello() {
        echo "Hello, {$this->name}!";

$greeting = new Greeting();

Flight::route('/', array($greeting, 'hello')); 

Routes are matched in the order they are defined. The first route to match a request will be invoked.

Method Routing

By default, route patterns are matched against all request methods. You can respond to specific methods by placing an identifier before the URL.

Flight::route('GET /', function(){
    echo 'I received a GET request.';

Flight::route('POST /', function(){
    echo 'I received a POST request.';

You can also map multiple methods to a single callback by using a | delimiter:

Flight::route('GET|POST /', function(){
    echo 'I received either a GET or a POST request.';

Regular Expressions

You can use regular expressions in your routes:

Flight::route('/user/[0-9]+', function(){
    // This will match /user/1234

Named Parameters

You can specify named parameters in your routes which will be passed along to your callback function.

Flight::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";

You can also include regular expressions with your named parameters by using the : delimiter:

Flight::route('/@name/@id:[0-9]{3}', function($name, $id){
    // This will match /bob/123
    // But will not match /bob/12345

Matching regex groups () with named parameters isn't supported.

Optional Parameters

You can specify named parameters that are optional for matching by wrapping segments in parentheses.

Flight::route('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
    // This will match the following URLS:
    // /blog/2012/12/10
    // /blog/2012/12
    // /blog/2012
    // /blog

Any optional parameters that are not matched will be passed in as NULL.


Matching is only done on individual URL segments. If you want to match multiple segments you can use the * wildcard.

Flight::route('/blog/*', function(){
    // This will match /blog/2000/02/01

To route all requests to a single callback, you can do:

Flight::route('*', function(){
    // Do something


You can pass execution on to the next matching route by returning true from your callback function.

Flight::route('/user/@name', function($name){
    // Check some condition
    if ($name != "Bob") {
        // Continue to next route
        return true;

Flight::route('/user/*', function(){
    // This will get called

Route Info

If you want to inspect the matching route information, you can request for the route object to be passed to your callback by passing in true as the third parameter in the route method. The route object will always be the last parameter passed to your callback function.

Flight::route('/', function($route){
    // Array of HTTP methods matched against

    // Array of named parameters

    // Matching regular expression

    // Contains the contents of any '*' used in the URL pattern
}, true);


Flight is designed to be an extensible framework. The framework comes with a set of default methods and components, but it allows you to map your own methods, register your own classes, or even override existing classes and methods.

Mapping Methods

To map your own custom method, you use the map function:

// Map your method
Flight::map('hello', function($name){
    echo "hello $name!";

// Call your custom method

Registering Classes

To register your own class, you use the register function:

// Register your class
Flight::register('user', 'User');

// Get an instance of your class
$user = Flight::user();

The register method also allows you to pass along parameters to your class constructor. So when you load your custom class, it will come pre-initialized. You can define the constructor parameters by passing in an additional array. Here's an example of loading a database connection:

// Register class with constructor parameters
Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'));

// Get an instance of your class
// This will create an object with the defined parameters
//     new PDO('mysql:host=localhost;dbname=test','user','pass');
$db = Flight::db();

If you pass in an additional callback parameter, it will be executed immediately after class construction. This allows you to perform any set up procedures for your new object. The callback function takes one parameter, an instance of the new object.

// The callback will be passed the object that was constructed
Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'), function($db){

By default, every time you load your class you will get a shared instance. To get a new instance of a class, simply pass in false as a parameter:

// Shared instance of the class
$shared = Flight::db();

// New instance of the class
$new = Flight::db(false);

Keep in mind that mapped methods have precedence over registered classes. If you declare both using the same name, only the mapped method will be invoked.


Flight allows you to override its default functionality to suit your own needs, without having to modify any code.

For example, when Flight cannot match a URL to a route, it invokes the notFound method which sends a generic HTTP 404 response. You can override this behavior by using the map method:

Flight::map('notFound', function(){
    // Display custom 404 page
    include 'errors/404.html';

Flight also allows you to replace core components of the framework. For example you can replace the default Router class with your own custom class:

// Register your custom class
Flight::register('router', 'MyRouter');

// When Flight loads the Router instance, it will load your class
$myrouter = Flight::router();

Framework methods like map and register however cannot be overridden. You will get an error if you try to do so.


Flight allows you to filter methods before and after they are called. There are no predefined hooks you need to memorize. You can filter any of the default framework methods as well as any custom methods that you've mapped.

A filter function looks like this:

function(&$params, &$output) {
    // Filter code

Using the passed in variables you can manipulate the input parameters and/or the output.

You can have a filter run before a method by doing:

Flight::before('start', function(&$params, &$output){
    // Do something

You can have a filter run after a method by doing:

Flight::after('start', function(&$params, &$output){
    // Do something

You can add as many filters as you want to any method. They will be called in the order that they are declared.

Here's an example of the filtering process:

// Map a custom method
Flight::map('hello', function($name){
    return "Hello, $name!";

// Add a before filter
Flight::before('hello', function(&$params, &$output){
    // Manipulate the parameter
    $params[0] = 'Fred';

// Add an after filter
Flight::after('hello', function(&$params, &$output){
    // Manipulate the output
    $output .= " Have a nice day!";

// Invoke the custom method
echo Flight::hello('Bob');

This should display:

Hello Fred! Have a nice day!

If you have defined multiple filters, you can break the chain by returning false in any of your filter functions:

Flight::before('start', function(&$params, &$output){
    echo 'one';

Flight::before('start', function(&$params, &$output){
    echo 'two';

    // This will end the chain
    return false;

// This will not get called
Flight::before('start', function(&$params, &$output){
    echo 'three';

Note, core methods such as map and register cannot be filtered because they are called directly and not invoked dynamically.


Flight allows you to save variables so that they can be used anywhere in your application.

// Save your variable
Flight::set('id', 123);

// Elsewhere in your application
$id = Flight::get('id');

To see if a variable has been set you can do:

if (Flight::has('id')) {
     // Do something

You can clear a variable by doing:

// Clears the id variable

// Clears all variables

Flight also uses variables for configuration purposes.

Flight::set('flight.log_errors', true);


Flight provides some basic templating functionality by default. To display a view template call the render method with the name of the template file and optional template data:

Flight::render('hello.php', array('name' => 'Bob'));

The template data you pass in is automatically injected into the template and can be reference like a local variable. Template files are simply PHP files. If the content of the hello.php template file is:

Hello, '<?php echo $name; ?>'!

The output would be:

Hello, Bob!

You can also manually set view variables by using the set method:

Flight::view()->set('name', 'Bob');

The variable name is now available across all your views. So you can simply do:


Note that when specifying the name of the template in the render method, you can leave out the .php extension.

By default Flight will look for a views directory for template files. You can set an alternate path for your templates by setting the following config:

Flight::set('flight.views.path', '/path/to/views');


It is common for websites to have a single layout template file with interchanging content. To render content to be used in a layout, you can pass in an optional parameter to the render method.

Flight::render('header', array('heading' => 'Hello'), 'header_content');
Flight::render('body', array('body' => 'World'), 'body_content');

Your view will then have saved variables called header_content and body_content. You can then render your layout by doing:

Flight::render('layout', array('title' => 'Home Page'));

If the template files looks like this:


<h1><?php echo $heading; ?></h1>


<div><?php echo $body; ?></div>


<title><?php echo $title; ?></title>
<?php echo $header_content; ?>
<?php echo $body_content; ?>

The output would be:

<title>Home Page</title>

Custom Views

Flight allows you to swap out the default view engine simply by registering your own view class. Here's how you would use the Smarty template engine for your views:

// Load Smarty library
require './Smarty/libs/Smarty.class.php';

// Register Smarty as the view class
// Also pass a callback function to configure Smarty on load
Flight::register('view', 'Smarty', array(), function($smarty){
    $smarty->template_dir = './templates/';
    $smarty->compile_dir = './templates_c/';
    $smarty->config_dir = './config/';
    $smarty->cache_dir = './cache/';

// Assign template data
Flight::view()->assign('name', 'Bob');

// Display the template

For completeness, you should also override Flight's default render method:

Flight::map('render', function($template, $data){

Error Handling

Errors and Exceptions

All errors and exceptions are caught by Flight and passed to the error method. The default behavior is to send a generic HTTP 500 Internal Server Error response with some error information.

You can override this behavior for your own needs:

Flight::map('error', function(Exception $ex){
    // Handle error
    echo $ex->getTraceAsString();

By default errors are not logged to the web server. You can enable this by changing the config:

Flight::set('flight.log_errors', true);

Not Found

When a URL can't be found, Flight calls the notFound method. The default behavior is to send an HTTP 404 Not Found response with a simple message.

You can override this behavior for your own needs:

Flight::map('notFound', function(){
    // Handle not found


You can redirect the current request by using the redirect method and passing in a new URL:


By default Flight sends a HTTP 303 status code. You can optionally set a custom code:

Flight::redirect('/new/location', 401);


Flight encapsulates the HTTP request into a single object, which can be accessed by doing:

$request = Flight::request();

The request object provides the following properties:

url - The URL being requested
base - The parent subdirectory of the URL
method - The request method (GET, POST, PUT, DELETE)
referrer - The referrer URL
ip - IP address of the client
ajax - Whether the request is an AJAX request
scheme - The server protocol (http, https)
user_agent - Browser information
type - The content type
length - The content length
query - Query string parameters
data - Post data or JSON data
cookies - Cookie data
files - Uploaded files
secure - Whether the connection is secure
accept - HTTP accept parameters
proxy_ip - Proxy IP address of the client
host - The request host name

You can access the query, data, cookies, and files properties as arrays or objects.

So, to get a query string parameter, you can do:

$id = Flight::request()->query['id'];

Or you can do:

$id = Flight::request()->query->id;

RAW Request Body

To get the raw HTTP request body, for example when dealing with PUT requests, you can do:

$body = Flight::request()->getBody();

JSON Input

If you send a request with the type application/json and the data {"id": 123} it will be available from the data property:

$id = Flight::request()->data->id;

HTTP Caching

Flight provides built-in support for HTTP level caching. If the caching condition is met, Flight will return an HTTP 304 Not Modified response. The next time the client requests the same resource, they will be prompted to use their locally cached version.


You can use the lastModified method and pass in a UNIX timestamp to set the date and time a page was last modified. The client will continue to use their cache until the last modified value is changed.

Flight::route('/news', function(){
    echo 'This content will be cached.';


ETag caching is similar to Last-Modified, except you can specify any id you want for the resource:

Flight::route('/news', function(){
    echo 'This content will be cached.';

Keep in mind that calling either lastModified or etag will both set and check the cache value. If the cache value is the same between requests, Flight will immediately send an HTTP 304 response and stop processing.


You can stop the framework at any point by calling the halt method:


You can also specify an optional HTTP status code and message:

Flight::halt(200, 'Be right back...');

Calling halt will discard any response content up to that point. If you want to stop the framework and output the current response, use the stop method:



Flight provides support for sending JSON and JSONP responses. To send a JSON response you pass some data to be JSON encoded:

Flight::json(array('id' => 123));

For JSONP requests you, can optionally pass in the query parameter name you are using to define your callback function:

Flight::jsonp(array('id' => 123), 'q');

So, when making a GET request using ?q=my_func, you should receive the output:


If you don't pass in a query parameter name it will default to jsonp.


You can customize certain behaviors of Flight by setting configuration values through the set method.

Flight::set('flight.log_errors', true);

The following is a list of all the available configuration settings:

flight.base_url - Override the base url of the request. (default: null)
flight.case_sensitive - Case sensitive matching for URLs. (default: false)
flight.handle_errors - Allow Flight to handle all errors internally. (default: true)
flight.log_errors - Log errors to the web server's error log file. (default: false)
flight.views.path - Directory containing view template files. (default: ./views)
flight.views.extension - View template file extension. (default: .php)

Framework Methods

Flight is designed to be easy to use and understand. The following is the complete set of methods for the framework. It consists of core methods, which are regular static methods, and extensible methods, which are mapped methods that can be filtered or overridden.

Core Methods

Flight::map($name, $callback) // Creates a custom framework method.
Flight::register($name, $class, [$params], [$callback]) // Registers a class to a framework method.
Flight::before($name, $callback) // Adds a filter before a framework method.
Flight::after($name, $callback) // Adds a filter after a framework method.
Flight::path($path) // Adds a path for autoloading classes.
Flight::get($key) // Gets a variable.
Flight::set($key, $value) // Sets a variable.
Flight::has($key) // Checks if a variable is set.
Flight::clear([$key]) // Clears a variable.
Flight::init() // Initializes the framework to its default settings.
Flight::app() // Gets the application object instance

Extensible Methods

Flight::start() // Starts the framework.
Flight::stop() // Stops the framework and sends a response.
Flight::halt([$code], [$message]) // Stop the framework with an optional status code and message.
Flight::route($pattern, $callback) // Maps a URL pattern to a callback.
Flight::redirect($url, [$code]) // Redirects to another URL.
Flight::render($file, [$data], [$key]) // Renders a template file.
Flight::error($exception) // Sends an HTTP 500 response.
Flight::notFound() // Sends an HTTP 404 response.
Flight::etag($id, [$type]) // Performs ETag HTTP caching.
Flight::lastModified($time) // Performs last modified HTTP caching.
Flight::json($data, [$code], [$encode], [$charset], [$option]) // Sends a JSON response.
Flight::jsonp($data, [$param], [$code], [$encode], [$charset], [$option]) // Sends a JSONP response.

Any custom methods added with map and register can also be filtered.

Framework Instance

Instead of running Flight as a global static class, you can optionally run it as an object instance.

require 'flight/autoload.php';

use flight\Engine;

$app = new Engine();

$app->route('/', function(){
    echo 'hello world!';


So instead of calling the static method, you would call the instance method with the same name on the Engine object.

Download Details:

Author: Mikecao
Source Code: 
License: MIT license

#php #micro #framework #restful