We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[solved] Micro API: strange behavior with dot values with 4.0

Hello,

I am trying to "PUT" values like FQDN with an API, but I got 404 ... I have written a brief testcase to reproduce it with a simple GET request.. Can I have some help to point me my error ?

I initialized the project with:

$ phalcon create-project store micro

Phalcon DevTools (4.0.3)

  Success: Project 'store' was successfully created.

  Info: Please choose a password and username to use Database connection.

  Info: Used default: 'root' without password.

Then I edited the app.php with:

<?php

use Phalcon\Mvc\Micro;

/**
 * Local variables
 * @var \Phalcon\Mvc\Micro $app
 */

$app = new Micro();

/**
 * Add your routes here
 */
$app->get('/', function () {
    echo 'index' .PHP_EOL;
});

// stupid function to check
$app->get(
    '/invoices/view/{id}',
    function ($id) {
        echo "Invoice #{$id}!" . PHP_EOL;
    }
);

/**
 * Not found handler
 */
$app->notFound(function () use($app) {
    $app->response->setStatusCode(404, "Not Found")->sendHeaders();
    echo '404' . PHP_EOL;
});

Then I launch a php server process:

$ phalcon serve --hostname=127.0.0.1 --port=8091  --rootpath=public --basepath=public/index.php

and finally i test using curl:

$ curl https://127.0.0.1:8091/invoices/view/1
Invoice #1!
$ curl https://127.0.0.1:8091/invoices/view/1.1
<!doctype html><html><head><title>404 Not Found</title><style>
body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; }
h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; }
h1, p { padding-left: 10px; }
code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}
</style>
</head><body><h1>Not Found</h1><p>The requested resource <code class="url">/invoices/view/1.1</code> was not found on this server.</p></body></html>
$

The only difference is 1.1 instead of 1 ... Is there a parameter missing ? a filter problem perhaps ?

Thank you for your help ... Nicolas.

$ phalcon info

Phalcon DevTools (4.0.3)

Environment:
  OS: Darwin MacBook-Air-de-Nicolas.local 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64
  PHP Version: 7.2.30
  PHP SAPI: cli
  PHP Bin: /usr/local/Cellar/[email protected]/7.2.30_1/bin/php
  PHP Extension Dir: /usr/local/Cellar/[email protected]/7.2.30_1/lib/php/20170718
  PHP Bin Dir: /usr/local/Cellar/[email protected]/7.2.30_1/bin
  Loaded PHP config: /usr/local/etc/php/7.2/php.ini
Versions:
  Phalcon DevTools Version: 4.0.3
  Phalcon Version: 4.0.5
  AdminLTE Version: 2.3.6

My guess is that 1.1 doesn't match the built-in regex because of the ..

Try it with: /invoices/view/{id:[0-9\.]+} which will allow numbers and periods.

Thank you.

I tried

$app->get(
    '/invoices/view/{id:[0-9\.]+}',
    function ($id) {
        echo "Invoice #{$id}!" . PHP_EOL;
    }
);

but the dot is still not allowed. I tried using [[:graph:]]+ too, without success:

$ curl https://127.0.0.1:8091/invoices/view/2.2
<!doctype html><html><head><title>404 Not Found</title><style>
body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; }
h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; }
h1, p { padding-left: 10px; }
code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}
</style>
</head><body><h1>Not Found</h1><p>The requested resource <code class="url">/invoices/view/2.2</code> was not found on this server.</p></body></html>

BUT, trying something like '/invoices/view/{id:2[0-9\.]+}', is working as expected:

$ curl https://127.0.0.1:8091/invoices/view/1
404
$ curl https://127.0.0.1:8091/invoices/view/2
404
$ curl https://127.0.0.1:8091/invoices/view/11
404
$ curl https://127.0.0.1:8091/invoices/view/21
Invoice #21!

I am pretty sure that this is a matching error ...



8.4k
edited Jul '20

i tested it with 3.4 and the result was as expected

/invoices/view/2.2 -> Invoice #2.2!

but i didn't use phalcon serve

i tested it again with 4.0.0

at first i got 404 then i changed the public/index.php there was an issue with phalcon not being as root folder you can read about it here

and i got the expected result /invoices/view/2.2 -> Invoice #2.2!

i didn't use the regex that @quasipickle suggested

maybe editing the index.php file and using apache would fix the problem



223
Accepted
answer
edited Jul '20

Hi,

the link provided is a part of the solution :)

As @talal424 said, it is working with apache, and probably not with phalcon devtools serve command. I modified the code as suggested (more or less).

For posterity, here is my code which finally work with dots.

public/index.php:

// snip
    /**
     * Starting the application
     * Assign service locator to the application
     */
    $app = new Micro($di);

    /**
     * Include Application
     */
    include APP_PATH . '/app.php';

    /**
     * DEBUG ENV
     */
    foreach ($_SERVER as $s => $v) {
        echo '_SERVER: ' . $s . ' => ' . $v .PHP_EOL;
    }
    foreach ($_GET as $s => $v) {
        echo '_GET: ' . $s . ' => ' . $v .PHP_EOL;
    }
    // end of debug

    /**
     * Handle the request
     */
    $app->handle($_GET['_url'] ?? '/');
// snip

Note $app->handle($_GET['_url'] ?? '/');

app/app.php is now:

$app->get(
    '/invoices/view/{id}',
    function ($id) {
        echo "Invoice #{$id}!" . PHP_EOL;
    }
);

I did not modified baseUri in config.php, because for my test, I do not generate any links.

As suggested, i saw that generated .htaccess is:

RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

But phalcon devtools is not apache, and this file is not read. And because I am on my dev host, without apache, I have to pass _url as a query string, because it is the default behavior for phalcon. I did not modified the cli to start phalcon:

$ phalcon serve --hostname=127.0.0.1 --port=8091  --rootpath=public --basepath=public/index.php

BUT my tests are now:

$ curl https://127.0.0.1:8091/?_url=/invoices/view/2.2
_SERVER: DOCUMENT_ROOT => /Users/nicolas/git/phalcontesting/store/public
_SERVER: REMOTE_ADDR => 127.0.0.1
_SERVER: REMOTE_PORT => 56142
_SERVER: SERVER_SOFTWARE => PHP 7.2.30 Development Server
_SERVER: SERVER_PROTOCOL => HTTP/1.1
_SERVER: SERVER_NAME => 127.0.0.1
_SERVER: SERVER_PORT => 8091
_SERVER: REQUEST_URI => /?_url=/invoices/view/2.2
_SERVER: REQUEST_METHOD => GET
_SERVER: SCRIPT_NAME => /index.php
_SERVER: SCRIPT_FILENAME => /Users/nicolas/git/phalcontesting/store/public/index.php
_SERVER: PHP_SELF => /index.php
_SERVER: QUERY_STRING => _url=/invoices/view/2.2
_SERVER: HTTP_HOST => 127.0.0.1:8091
_SERVER: HTTP_USER_AGENT => curl/7.64.1
_SERVER: HTTP_ACCEPT => */*
_SERVER: REQUEST_TIME_FLOAT => 1595712855.0253
_SERVER: REQUEST_TIME => 1595712855
_GET: _url => /invoices/view/2.2
Invoice #2.2!

And it is working as expected.

Thank you all for your help. Hope it will help other people ;)

Nota bene: it helped me fixing a codeception config:

actor: ApiTester
modules:
    enabled:
        - REST:
            url: https://localhost:8090/?_url=/api/v1/
            depends: PhpBrowser
            part: Json
        - Db:
            dsn: "mysql:host=localhost:5726;dbname=b2nadmin"
            user: msandbox
            password: msandbox

Indeed, this is not a phalcon bug :)

https://bugs.php.net/bug.php?id=61286

and it is not fixed/corrected yet: https://github.com/php/php-src/pull/3215