示例#1
0
<?php

require_once realpath('../../') . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$client = new Predis\Async\Client('tcp://127.0.0.1:6379', $loop);
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);
$session_store = new Phluid\Middleware\Sessions\PredisStore($client);
$app = new Phluid\App();
$app->inject(new Phluid\Middleware\Cookies());
$app->inject(new Phluid\Middleware\Sessions(array('store' => $session_store, 'secret' => 'aslkji339jkcmas0o329insdlsdoisdf0s09jasfd')));
$app->get('/', function ($req, $res) {
    if ($count = $req->session['counter']) {
        $count++;
    } else {
        $count = 1;
    }
    $req->session['counter'] = $count;
    $res->renderText("Hello world ;): {$count}");
});
$app->createServer($http);
$socket->listen(4000);
$loop->run();
示例#2
0
文件: App.php 项目: AlexSJ/phluid-php
<?php

require_once realpath('../') . '/vendor/autoload.php';
$app = new Phluid\App(array('default_layout' => 'layout'));
$app->inject(new \Phluid\Middleware\RequestTimer());
$app->inject(function ($request, $response, $next) {
    $response->once('end', function () use($request, $response) {
        echo "{$request} {$response} in {$request->duration} ms" . PHP_EOL;
    });
    $next();
});
$app->inject(function ($req, $res, $next) {
    $res->setHeader('X-Powered-By', 'Awesomesauce');
    $next();
});
$app->inject(new \Phluid\Middleware\ExceptionHandler());
$app->inject(new \Phluid\Middleware\BasicAuth(function ($credentials, $success, $failure) {
    $username = $credentials['username'];
    $password = $credentials['password'];
    if (strtolower($username) == 'admin' && $password == "secret") {
        $success($username);
    } else {
        $failure();
    }
}));
$app->inject(new \Phluid\Middleware\StaticFiles(__DIR__ . '/public'));
$app->inject(function ($req, $res, $next) {
    $reverse = false;
    $new_path = preg_replace('/\\/reverse\\/?$/', '/', $req->path);
    if ($new_path !== $req->path) {
        $req->path = $new_path;