leocavalcante / siler

⚡ Flat-files and plain-old PHP functions rockin'on as a set of general purpose high-level abstractions.
https://siler.leocavalcante.dev
MIT License
1.12k stars 90 forks source link

API returns 'Not Found' after first request on v1.7.9 #556

Open leyume opened 3 years ago

leyume commented 3 years ago

Hello @leocavalcante Thank you for Siler.

There's this strange issue with v1.7.9 API returns 'Not Found' after first request with Siler Swoole

This worked fine up to version 1.7.8


Simple code to recreate

index.php

<?php 
declare(strict_types=1);
require_once 'vendor/autoload.php';

use Siler\Swoole;
use Siler\Route;

$server = function () {
    Swoole\cors( '*', 'Authorization, Content-Type', 'GET,POST,PUT' );

    try {
        Route\files('api');
    } catch (Exception $e) {
        Resource::error($e->errorInfo[2]);
    }

    Swoole\emit('Not found', 404);
};

Swoole\http($server)->start();

Then, api/get.php

<?php
use Siler\Swoole;

return function () {
    Swoole\json( ['greet'=>'Hello', 'name'=>'Leo'] );
};

Request API more than once. You would see the "Not Found"

ogfremo commented 3 years ago

I encountered this behavior while following the official documentation.

I added a second emit to verify it.

The first visit returns the route properly, at the second attempt it bypasses all other Routes and triggers the first Swoole\emit it encounters so in my case it returns 'Hello World' rather than 'Not Found'.

index.php:

<?php declare(strict_types=1);

require_once 'vendor/autoload.php';

use Siler\Swoole;
use Siler\Route;
use Siler\Twig;

Twig\init('pages');

$handler = function ($req) {
  Route\get('/', 'pages/home.php');
  Route\get('/todos', 'api/todos.php');
  Swoole\emit('Hello World');
  Swoole\emit('Not found', 404);
};

Swoole\http($handler)->start();

pages/home.php

<?php declare(strict_types=1);

use Siler\Swoole;
use Siler\Twig;

return fn() => Swoole\emit(Twig\render('home.twig'));

api/todos.php

<?php declare(strict_types=1);

use Siler\Swoole;

return function () {
    Swoole\cors();
    Swoole\json([
      ['id' => 1, 'text' => 'foo'],
      ['id' => 2, 'text' => 'bar'],
      ['id' => 3, 'text' => 'baz'],
  ]);
};
jrborbars commented 3 years ago

Exactly same behavior for me. Using fresh installed swoole with pecl and php 7.4, siler from composer.