Git Product home page Git Product logo

bridge-swoole's Introduction

Phalcon ๐ŸŒ‰ Swoole

Bridge to run Phalcon with Swoole.

Installation

composer require phalcon/bridge-swoole

Quick example

See comments inside code for more details.

<?php

declare(strict_types=1);

use Phalcon\Mvc\Micro;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;

$http = new Server('0.0.0.0', 9501);
$http->on('start', function () {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on('request', function (Request $request, Response $response) {
    $app = new Micro();
    $app->setService('request', new \Phalcon\Bridge\Swoole\Request($request), true);
    $app->setService('response', new \Phalcon\Bridge\Swoole\Response($response), true);
    /**
     * We need to define our response handler, because
     * default Micro response handler will send output
     * into client, meanwhile we just want it to pass
     * to the Swoole's response end() method.
     */
    $app->setResponseHandler(function () use ($app) {
        return $app->response->getContent();
    });

    /**
     * Without fallback 404 handler it will crush.
     */
    $app->notFound(function () use ($app) {
        $app
            ->response
            ->setStatusCode(404, 'Not Found')
            ->setContent('Not found');
    });

    /**
     * Define GET endpoint.
     */
    $app->get('/', function() use ($app) {
        $app
            ->response
            ->setContent('Hello World');
    });

    /**
     * Handle in Phalcon the request and pick response content.
     * Then pass to Swoole and end response. 
     */
    $content = $app->handle($request->server['request_uri']);
    $response->end($content);
});

$http->start();

bridge-swoole's People

Contributors

jeckerson avatar sinbadxiii avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.