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  
Name: Yousaf Syed <contact>
Classes: 6 packages by
Country: Pakistan Pakistan
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

  Files folder image Files (62)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (62)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file build.yaml Data Auxiliary data

  Files folder image Files (62)  /  Crow  
File Role Description
Files folder imageHandlers (7 files)
Files folder imageHttp (5 files, 1 directory)
Files folder imageMiddlewares (4 files)
Files folder imageRouter (5 files, 1 directory)

  Files folder image Files (62)  /  Crow  /  Handlers  
File Role Description
  Plain text file CrowRequestHandler.php Class Class source
  Plain text file ErrorHandler.php Class Class source
  Plain text file QueueRequestHandler.php Class Class source
  Plain text file QueueRequestHandlerBuilder.php Class Class source
  Plain text file ReactRequestHandler.php Class Class source
  Plain text file RouteDispatchHandler.php Class Class source
  Plain text file SwooleRequestHandler.php Class Class source

  Files folder image Files (62)  /  Crow  /  Http  
File Role Description
Files folder imageServer (7 files, 1 directory)
  Plain text file DefaultHeaders.php Class Class source
  Plain text file PsrToSwooleResponseBuilder.php Class Class source
  Plain text file RequestFactory.php Class Class source
  Plain text file ResponseBuilder.php Class Class source
  Plain text file SwooleRequest.php Class Class source

  Files folder image Files (62)  /  Crow  /  Http  /  Server  
File Role Description
Files folder imageExceptions (2 files)
  Plain text file BaseServer.php Class Class source
  Plain text file CrowReactServer.php Class Class source
  Plain text file CrowSwooleServer.php Class Class source
  Plain text file Factory.php Class Class source
  Plain text file ReactPHPServer.php Class Class source
  Plain text file ServerInterface.php Class Class source
  Plain text file SwoolePHPServer.php Class Class source

  Files folder image Files (62)  /  Crow  /  Http  /  Server  /  Exceptions  
File Role Description
  Plain text file InvalidEventType.php Class Class source
  Plain text file InvalidServerType.php Class Class source

  Files folder image Files (62)  /  Crow  /  Middlewares  
File Role Description
  Plain text file ErrorMiddleware.php Class Class source
  Plain text file FinalMiddleware.php Class Class source
  Plain text file RoutingMiddleware.php Class Class source
  Plain text file UserMiddlewaresList.php Class Class source

  Files folder image Files (62)  /  Crow  /  Router  
File Role Description
Files folder imageExceptions (1 file)
  Plain text file DispatcherFactoryInterface.php Class Class source
  Plain text file Factory.php Class Class source
  Plain text file FastRouteDispatcher.php Class Class source
  Plain text file FastRouter.php Class Class source
  Plain text file RouterInterface.php Class Class source

  Files folder image Files (62)  /  Crow  /  Router  /  Exceptions  
File Role Description
  Plain text file RoutingLogicException.php Class Class source

  Files folder image Files (62)  /  examples  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (62)  /  Tests  
File Role Description
Files folder imageUnit (1 directory)

  Files folder image Files (62)  /  Tests  /  Unit  
File Role Description
Files folder imageCrow (4 directories)

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  
File Role Description
Files folder imageHandlers (6 files)
Files folder imageHttp (5 files, 1 directory)
Files folder imageMiddlewares (2 files)
Files folder imageRouter (3 files)

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  /  Handlers  
File Role Description
  Plain text file ErrorHandlerTest.php Class Class source
  Plain text file QueueRequestHandlerBuilderTest.php Class Class source
  Plain text file QueueRequestHandlerTest.php Class Class source
  Plain text file ReactRequestHandlerTest.php Class Class source
  Plain text file RouteDispatchHandlerTest.php Class Class source
  Plain text file SwooleRequestHandlerTest.php Class Class source

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  /  Http  
File Role Description
Files folder imageServer (5 files)
  Plain text file DefaultHeadersTest.php Class Class source
  Plain text file PsrToSwooleResponseBuilderTest.php Class Class source
  Plain text file RequestFactoryTest.php Class Class source
  Plain text file ResponseBuilderTest.php Class Class source
  Plain text file SwooleRequestTest.php Class Class source

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  /  Http  /  Server  
File Role Description
  Plain text file CrowReactServerTest.php Class Class source
  Plain text file CrowSwooleServerTest.php Class Class source
  Plain text file FactoryTest.php Class Class source
  Plain text file ReactPHPServerTest.php Class Class source
  Plain text file SwoolePHPServerTest.php Class Class source

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  /  Middlewares  
File Role Description
  Plain text file ErrorMiddlewareTest.php Class Class source
  Plain text file RoutingMiddlewareTest.php Class Class source

  Files folder image Files (62)  /  Tests  /  Unit  /  Crow  /  Router  
File Role Description
  Plain text file FactoryTest.php Class Class source
  Plain text file FastRouteDispatcherTest.php Class Class source
  Plain text file FastRouterTest.php Class Class source

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