Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson authored Jun 7, 2024
1 parent 71319e7 commit 7943d0d
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,75 @@ See comments inside code for more details.

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) {
$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();
});
$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 ($app) {
$app
->response
->setStatusCode(404, 'Not Found')
->setContent('Not found');
});

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

Expand Down

0 comments on commit 7943d0d

Please sign in to comment.