Slim framework - cannot interpret routes with dot

余生颓废 提交于 2019-11-29 12:11:56

Slim relies on $_SERVER['SCRIPT_NAME'] which is supposed to be the routing file (Apache's DirectoryIndex, and the file where the Rewrite points to. Let's say index.php for the example below).

In PHP embedded server, if the URI contains a dot in the file part, then PHP does not position properly the SCRIPT_NAME value.

You can use the following tweak to mimic the RewriteRule behavior:

Create a router.php file and send all the requests through it with your PHP embedded server:

$_SERVER['SCRIPT_NAME'] = 'index.php';
include 'index.php';

Then, aunch the PHP embedded server with:

php -S 0.0.0.0:<port> -t <your-Http-document-root> router.php

The router.php file will not be in use in your production environment. Only the PHP embedded server uses it, and it should solve your "Routes With Dots" issue.

Root issue - PHP Development server

After playing with different Slim conditions and experiments, I started to look closer at the server level and making sure that the URL was passed correctly to the Slim routes.

Turns out this was an issue with PHP's development server, which is available through the php -S command.

A quick google search revealed that several others had encountered the same issue, and it was a server issue, rather than a bug in Slim.

Solution

To test out my solution, I ran MAMP, and transferred all my files over. And then I wrote the .htaccess to redirect all requests through to index.php.

The moment of truth:

I typed in localhost:8888/campaigns/demo.com/12 and a wave of joy surged through my veins as I saw a line of beautiful, gorgeous demo.com/12 written across the browser! (I have an echo statement for that particular route) A week-plus of troubleshooting and tinkering around has finally bore fruit!

Celebratory Hoorah!

\(^O^)/ Hoorah for Apache!

Thanks for your help @adosaiguas!

I would give a try to slim's Route Conditions. You should be able to specify something like this to allow dots in your parameter:

$app->get('/:domain/:id', function($domain, $id) {
    echo $domain . ' ' . $id;
})->conditions(array('domain' => '[a-zA-Z\.]+'));

If this does not work, you should think on using another character instead of a dot to separate the domain name from the TLD and then replace it in the function:

$app->get('/:domain/:id', function($domain, $id) {
    $domain = str_replace('_', '.', $domain);
    echo $domain . ' ' . $id;
})->conditions(array('domain' => '[a-zA-Z_]+'));

It is not so elegant, but it works.

I've just added this to the top of router script (index.php in my case):

if (PHP_SAPI === 'cli-server') {
  $_SERVER['SCRIPT_NAME'] = pathinfo(__FILE__, PATHINFO_BASENAME);
}

Usage example:

$ php -S localhost:8080 -t public index.php
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!