Code Monkey home page Code Monkey logo

yii2-psr7-bridge's Issues

support sendFile and sendStreamAsFile for Response

support sendFile and sendStreamAsFile for Response, it is ok with workerman ,hope to provide an idea,

use GuzzleHttp\Psr7\Utils;

class Response extends \yii\Psr7\web\Response
{
    /**
     * {@inheritDoc}
     */
    public function sendStreamAsFile($handle, $attachmentName, $options = [])
    {
        $response = parent::sendStreamAsFile($handle, $attachmentName, $options);
        $this->stream = Utils::streamFor($this->stream[0]);
        return $response;
    }
}

Bug in Application->handleError

 /**
     * Handles exceptions and errors thrown by the request handler
     *
     * @param \Throwable|\Exception $exception
     * @return ResponseInterface
     */
    private function handleError(\Throwable $exception) : ResponseInterface
    {
        $response = $this->getErrorHandler()->handleException($exception);

        $this->trigger(self::EVENT_AFTER_REQUEST);
        $this->state = self::STATE_END;

        return $response->getPsr7Response();
    }

$this->getErrorHandler()->handleException() this is void method. it is not returned response

roadrunner configuration files .rr.yaml according to https://github.com/yiisoft/yii-runner-roadrunner/

version: '3'

server:
  command: "php ./worker.php"
  env:
    - YII_ALIAS_WEBROOT: "./web"
    - YII_ALIAS_WEB: "./"

rpc:
  listen: tcp://127.0.0.1:6001

http:
  address: :8088
  pool:
    num_workers: 8
    # Debug mode for the pool. In this mode, pool will not pre-allocate the worker.
    # Worker (only 1, num_workers ignored) will be allocated right after the request arrived.
    debug: false
  middleware: ["static", "headers"]
  static:
    dir:   "./web"
    forbid: [".php", ".htaccess"]
  headers:
    response:
      "Cache-Control": "no-cache"

logs:
  mode: production
  level: warn

route inoperative

composer.json

"require": {
        "spiral/roadrunner": "^2023.1.0",
        "spiral/roadrunner-http": "^v3.0.0",
        "spiral/roadrunner-worker": "^3.0.0",
        "spiral/roadrunner-cli": "^2.5.0",
        "spiral/roadrunner-grpc": "^3.0.0",
        "psr/http-message": "1.0.1",
        "charlesportwoodii/yii2-psr7-bridge": "dev-master",
        "psr/http-server-handler": "^1.0",
        "laminas/laminas-diactoros": "^2.3"
},
"replace": {
    "yidas/yii2-bower-asset": "2.0.13.1"
},

web.php

        'request' => [
            'class' => \yii\Psr7\web\Request::class,
            'cookieValidationKey' => '123456',
        ],
        'response' => [
            'class' => \yii\Psr7\web\Response::class
        ],

.rr.yaml

version: '3'

server:
  command: "php ./worker.php"
  env:
    - YII_ALIAS_WEBROOT: "./web"
    - YII_ALIAS_WEB: "./"

rpc:
  listen: tcp://127.0.0.1:6001

http:
  address: :8088
  pool:
    num_workers: 8
    # Debug mode for the pool. In this mode, pool will not pre-allocate the worker.
    # Worker (only 1, num_workers ignored) will be allocated right after the request arrived.
    debug: false
  middleware: ["static", "headers"]
  static:
    dir:   "./web"
    forbid: [".php", ".htaccess"]
  headers:
    response:
      "Cache-Control": "no-cache"

logs:
  mode: production
  level: warn

worker.php

#!/usr/bin/env php
<?php

ini_set('display_errors', 'stderr');

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'dev');

// Set your normal YII_ definitions
//defined('YII_DEBUG') or define('YII_DEBUG', true);
// Alternatives set this in your rr.yaml file
//defined('YII_DEBUG') or define('YII_DEBUG', \getenv('YII_DEBUG'));
// comment out the following two lines when deployed to production
//defined('YII_ENV') or define('YII_ENV', 'dev');
// Alternatives set this in your rr.yaml file
//defined('YII_ENV') or define('YII_ENV', \getenv('YII_ENV'));

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

$worker = Spiral\RoadRunner\Worker::create();
$psrServerFactory = new Laminas\Diactoros\ServerRequestFactory();
$psrStreamFactory = new Laminas\Diactoros\StreamFactory();
$psrUploadFileFactory = new Laminas\Diactoros\UploadedFileFactory();
$psr7 = new Spiral\RoadRunner\Http\PSR7Worker($worker, $psrServerFactory, $psrStreamFactory, $psrUploadFileFactory);

$config = require __DIR__ . '/config/web.php';

$application = (new \yii\Psr7\web\Application($config));

// Handle each request in a loop
try {
    while ($request = $psr7->waitRequest()) {
        if (($request instanceof Psr\Http\Message\ServerRequestInterface)) {
            try {
                $response = $application->handle($request);
                $psr7->respond($response);
            } catch (\Throwable $e) {
                $psr7->getWorker()->error((string)$e);
            }

            if ($application->clean()) {
                $psr7->getWorker()->stop();
                return;
            }
        }
    }
} catch (\Throwable $e) {
    $psr7->getWorker()->error((string)$e);
}

http://127.0.0.1:8088/?r=site%2Flogin

image

Ability to disable sessions

Hi!
How fully disable sessions (using authclient with REST)? It always sends cookie.

if (($session = $this->getSession()) !== null) {
// Close the session if it was open.
$session->close();
// If a session cookie is defined, load it into Yii::$app->session
if (isset($request->getCookieParams()[$session->getName()])) {
$session->setId($request->getCookieParams()[$session->getName()]);
}
}
// Open the session before any modules that need it are bootstrapped.
$this->ensureBehaviors();
$session->open();
$this->bootstrap();
// Once bootstrapping is done we can close the session.
// Accessing it in the future will re-open it.
$session->close();

Support of RoadRunner 2.0+

Hi, first thanks for this bridge.
Does this version work with RR 2.0+ ?
If yes, can you show me a code for the main loop in worker.php?
Thanks

URLManager Parameters not available in yii\web\Request::getQueryParams()

I have 'enablePrettyUrl' => true, 'showScriptName' => false, and 'news/<type:\w+>' => 'news/index' rule in my urlManager config. While using FPM approach, I can get type param (when accessing /news/some-type URL) by calling Yii::$app->request->get('type'). When using yii\Psr7\web\Request and your bridge, I get nulls on every get() or post() call.

Could you please describe how to get it working?

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.