GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - phalcon/bridge-swoole: Bridge to run Phalcon with Swoole.

BranchesTags
Open more actions menu

Latest commit

 

History

34 Commits
34 Commits

Folders and files

NameName
Last commit message
Last commit date
.docker
.docker
 
 
.examples
.examples
 
 
.github/workflows
.github/workflows
 
 
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
docker-compose.yml
docker-compose.yml
 
 

Repository files navigation

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\Di\Di;
use Phalcon\Mvc\Micro;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;

$di = new Di();
$di->setShared('router', new Router(false));

$app = new Micro($di);

/**
 * Define example GET endpoint with text response.
 */
$app->get('/', function() {
    return 'Hello World';
});

/**
 * Define example redirect.
 */
$app->get('/redirect', function () {
    // Redirect is handled by Swoole's Request.
    return ['redirect' => 'https://github.com'];
});

/**
 * Define example json response.
 */
$app->get('/json', function () {
    // Correct headers will be added from Swoole's Response.
    return ['json' => true];
});

$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) use ($app) {
    $app->setService('request', new \Phalcon\Bridge\Swoole\Request($request));
    $app->setService('response', new \Phalcon\Bridge\Swoole\Response($response));

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

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

    if (isset($content['content'])) {
        if (is_array($content['content'])) {
            $response->setHeader('Content-Type', 'application/json');
            $content = json_encode($content['content']);
        } else {
            $content = (string)$content['content'];
        }
    }

    $response->end($content);
});

$http->start();

About

Bridge to run Phalcon with Swoole.

Resources

Readme
MIT license

Security policy

Security policy
Activity
Custom properties

Stars

26 stars

Watchers

3 watching

Forks

4 forks
Report repository

Releases

Sponsor this project

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.