PHP Classes

Crow PHP Web Framework: Microservice framework for PHP

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 106 All time: 9,702 This week: 79Up
Version License PHP version Categories
crowphp 1.0.0MIT/X Consortium ...8Libraries, Design Patterns, PHP 8
Description 

Author

This package is a framework to develop applications using micro-services.

It allows creating an application server based on asynchronous programming using React PHP or using Swoole.

The package allows applications to register handlers to dispatch the expected types of HTTP requests.

It also routes the requests to the handle functions that were registered and the requests are received by the application server.

Innovation Award
PHP Programming Innovation award nominee
April 2021
Number 2


Prize: 1GB Cloud Hosting Server - free for 1 year
Asynchronous programming is a modern approach to develop applications that can make a more efficient use of the CPUs used in the machines on which the applications are running.

There are several solutions to implement asynchronous programming in PHP, like for instance Reach PHP and Swoole.

This package implements a framework that takes advantages of these asynchronous programming solutions to implement Web applications based on micro-services.

Manuel Lemos
Picture of Yousaf Syed
  Performance   Level  
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

 

Example

#!/usr/bin/env php
<?php
require 'vendor/autoload.php';

use
Psr\Http\Message\ResponseInterface;
use
Psr\Http\Message\ServerRequestInterface as RequestInterface;
use
Crow\Http\Server\Factory as CrowServer;
use
Crow\Router\RouterInterface;
use
Psr\Http\Server\RequestHandlerInterface;


$app = CrowServer::create(CrowServer::SWOOLE_SERVER);
$router = Crow\Router\Factory::make();

$router->get('/', function (RequestInterface $request, ResponseInterface $response) {
   
$key = 'crow\php';
    if (isset(
$request->getCookieParams()[$key])) {
       
$response->getBody()->write("Your cookie value is: " . $request->getCookieParams()[$key]);
        return
$response;
    }
   
$response->getBody()->write('Hello World home' . $request->getProtocolVersion());
    return
$response->withHeader(
       
'Set-Cookie',
       
urlencode($key) . '=' . urlencode('test;more'));
});

$router->get('/sleep5', function (RequestInterface $request, ResponseInterface $response) {
   
sleep(5);
   
$response->getBody()->write('Hello World after 5 seconds');
    return
$response;
});

$router->post('/file', function (RequestInterface $request, ResponseInterface $response) {
   
$files = $request->getUploadedFiles();
   
rename($files["screenshot_png"]["tmp_name"], "/tmp/" . $files["screenshot_png"]["name"]);
   
$response->getBody()->write('File Uploaded ' . $files["screenshot_png"]["name"]);
    return
$response;
});
$router->get('/id/{id}', function (RequestInterface $request, ResponseInterface $response, $id): ResponseInterface {
   
$response->getBody()->write('Hello World' . $id);
    return
$response;
})->
middleware(function (RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a local middleware 1\n";
    return
$next->handle($request);
})->
middleware(function (RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a local middleware 2\n";
    return
$next->handle($request);
});


$router->addGroup('/yousaf', function (RouterInterface $router) {
   
$router->get('/sunny/id/{id}', function (RequestInterface $request, ResponseInterface $response, $id): ResponseInterface {

       
$response->getBody()->write(json_encode(["message" => $id]));
        return
$response->withHeader('Content-Type', 'application/json')->withStatus(403);
    })->
middleware(function (RequestInterface $request, RequestHandlerInterface $next) {
        echo
"This is a local middleware 1 for sunny\n";
        return
$next->handle($request);
    });

   
$router->get('/mani/id/{id}', function (RequestInterface $request, ResponseInterface $response, $id): ResponseInterface {
       
$response->getBody()->write('Mani ' . $id);
        throw new
Exception('Hey i am an exception');
    });
   
$router->addGroup("/ehsan", function (RouterInterface $router) {
       
$router->get('/naqvi/id/{id}', function (RequestInterface $request, ResponseInterface $response, $id): ResponseInterface {
           
$response->getBody()->write('naqvi ' . $id);
            return
$response;
        });
    });

}, function (
RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a group middleware 1\n";
    return
$next->handle($request);
}, function (
RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a group middleware 2\n";
    return
$next->handle($request);
});

$app->withRouter($router);

$app->withTimeout(5);
//Uncaught Exceptions


$app->use(function (RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a global middleware 1\n";
    return
$next->handle($request);
});

$app->use(function (RequestInterface $request, RequestHandlerInterface $next) {
    echo
"This is a global middleware 2\n";
    return
$next->handle($request);
});

$app->on('workererror', function ($error) {
   
var_dump($error->getMessage());
});

$app->on('start', function ($server) {
    echo
"CrowPHP server is listening on port $server->host:$server->port " . PHP_EOL;
});

$app->configs(['reactor_num' => 2,
   
'worker_num' => 4,
   
'backlog' => 128,
   
'max_request' => 50,
   
'dispatch_mode' => 1,]);

$app->listen(5005, "0.0.0.0");


Details

Fast un-opinionated minimalist web framework and server for PHP built on top of Async PHP servers (SwoolePHP and ReactPHP). CrowPHP lets you build real microservices in PHP without the use of PHP-FPM/Nginx or Apache.

Build Status License Coverage

Installation

Requirements

  1. PHP >8.0
  2. Swoole PHP extension
$ pecl install swoole

Installation of CrowPHP via composer, the following command will install the framework and all of its dependencies with it.

composer install crowphp/crow

Hello world microservice using CrowPHP

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

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
use Crow\Http\Server\Factory as CrowServer;

$app = CrowServer::create(CrowServer::SWOOLE_SERVER);
$router = Crow\Router\Factory::make();

$router->get('/', function (RequestInterface $request, ResponseInterface $response) {
    $response->getBody()->write('Hello World');
    return $response;
});

$app->withRouter($router);

$app->listen(5005);

You may quickly test your newly built service as follows:

$ php index.php

Going to http://localhost:5005 will now display "Hello World".

For more information on how to configure your web server, see the Documentation.

Tests

To execute the test suite, you'll need to install all development dependencies.

$ git clone https://github.com/crowphp/crow
$ composer install
$ composer test

Contributing

Please see CONTRIBUTING for details.

Learn More

Learn more at these links: - Website

Security

If you discover security related issues, please email yousaf@bmail.pk or use the issue tracker.

License

The Crow Framework is licensed under the MIT license. See License File for more information.


  Files folder image Files (62)  
File Role Description
Files folder image.github (1 directory)
Files folder imageCrow (4 directories)
Files folder imageexamples (1 file)
Files folder imageTests (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpcs.xml.dist Data Auxiliary data
Accessible without login Plain text file phpstan.neon.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file Readme.md Doc. Read me
Accessible without login Plain text file SECURITY.md Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:106
This week:0
All time:9,702
This week:79Up
User Comments (1)
Great ideia to involve Swoole and focus on PHP 8 for this mic...
3 years ago (Carlos Artur Curvelo da Matos)
77%StarStarStarStar