Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

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

Open
leyume opened this issue Feb 6, 2021 · 2 comments
Open

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

leyume opened this issue Feb 6, 2021 · 2 comments
Assignees

Comments

@leyume
Copy link

leyume commented Feb 6, 2021

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"

@leyume leyume changed the title API returns 'Not Found' after first request API returns 'Not Found' after first request on v1.7.9 Feb 6, 2021
@leocavalcante leocavalcante self-assigned this Feb 7, 2021
@ogfremo
Copy link

ogfremo commented Apr 16, 2021

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants