超薄框架 - 无法解释带点的路线

Problem Statement

I'm currently working on an internal RESTful API, and I'm using our main domain name as an environment identifier. However, I noticed that Slim does not like it at all for a route to have dot in there.

Sample case

I have a local web server running using PHP's built-in webserver, and I invoked php -S 0.0.0.0:5000 to get it running.

Once the web server is up, I have a simple 'hello world' on the index page. Everything's working fine and dandy.

I then set up a route as following:

$app->get('/:domain/:id', function($domain, $id) 
{
    echo $domain . ' ' . $id;
} 

With this, I set up the route to hopefully resolve 0.0.0.0:5000/apple.com/juicers. I tried with 0.0.0.0:5000/apple/juicers and the page prints out apple juicers. But with 0.0.0.0:5000/apple.com/juicers, I get a 404 Not Found error.

What I've tried so far

URL Rewrite

I looked up the available resources on Google, and from Slim framework's Github issues as well. It seems like someone encountered a similar issue: https://github.com/codeguy/Slim/issues/359

The proposed solution in that ticket was: URL rewriting fix. I tried that but to no avail. I set up a .htaccess at the root of my project directory, and turned on AllowOverRide All in my Mac's httpd.conf file.

php -S 0.0.0.0:5000 index.php

Instead of running php -S 0.0.0.0:5000 by itself, I ran the command on index.php. At this point, instead of throwing a Not Found error, the slim framework is resolving to my base route, which I set up as follows:

$app->get('/', function() { echo "hello world"; });

Thank you

I'm at my wit's end, and any help will be highly appreciated. Thank you so much for reading!

4个回答



Root问题 - PHP开发服务器</ h3>

在玩了不同的Slim条件和实验后,我开始了 仔细查看服务器级别并确保URL已正确传递到Slim路由。 </ p>

原来这是PHP开发服务器的问题,可以通过 php -S </ code>命令获得。 </ p>

快速谷歌搜索显示其他几个人遇到了同样的问题,这是一个服务器问题,而不是Slim中的一个错误。 </ p>

解决方案</ h3>

为了测试我的解决方案,我运行了MAMP,并将我的所有文件都转移过来。 然后我写了.htaccess将所有请求重定向到index.php。 </ p>

关键时刻:</ p>

我输入 localhost:8888 / campaigns / demo.com / 12 </ code>和 当我在浏览器中看到一系列漂亮,华丽的 demo.com/12 </ code>时,欢快的浪潮席卷了我的血脉! (我对该特定路线有一个 echo </ code>语句)一​​周多的故障排除和修补工作终于结出硕果了!</ p>

庆祝Hoorah!</ h3>

\(^ O ^)/ Hoorah for Apache!</ p>

感谢您的帮助@adosaiguas!</ p>
</ div>

展开原文

原文

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!

douaipi3965
douaipi3965 在apache中,它可以立即使用。
6 年多之前 回复
douji5397
douji5397 所以问题在于php中的嵌入式web服务器? 在apache中,它是否需要条件或它是否立即可用?
6 年多之前 回复

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.

doubu5154
doubu5154 谢啦。 它救了我几天
2 年多之前 回复
dps123456789
dps123456789 fyr:github.com/slimphp/Slim/issues/1829和gist.github.com/ckressibucher/e37520cf2f1d08ec56d250c54d96ed7​​2
4 年多之前 回复
dsu89430
dsu89430 值得一提的一件事(发现困难的方法)是,你需要告诉router.php文件提供静态资产,如图像,js和css文件。 php文档中的示例#3显示了这个:php.net/manual/en / ...再次感谢您的回答!
大约 5 年之前 回复
doutao8774
doutao8774 请注意,问题仍存在于Slim 3中...
大约 5 年之前 回复
duanhao5038
duanhao5038 这非常有效。 谢谢!
大约 5 年之前 回复

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.

doushenjia8514
doushenjia8514 我已经更新了答案,因为代码没有编译。 此外,我已经设置了一个测试项目,你是正确的路线条件不起作用。 我认为我们可以将此视为框架的限制甚至是错误。 顺便说一句,SO上有这个问题的重复:stackoverflow.com/questions/18362793 / ...
6 年多之前 回复
duanpi5733
duanpi5733 这不起作用。 仍有相同的错误:在此服务器上找不到请求的资源/campaigns/hello.com/1。
6 年多之前 回复
dongzhan8001
dongzhan8001 那你错过了指定id? 像这样:0.0.0.0:5000 /campaigns / hello.com / 1
6 年多之前 回复
dsvjmc0907
dsvjmc0907 我输入了你上面的确切数字,这是我在浏览0.0.0.0:5000/campaigns/hello.com时收到的结果:在此服务器上找不到请求的资源/campaigns/hello.com。
6 年多之前 回复



我刚刚将它添加到路由器脚本的顶部(在我的情况下为index.php):</ p>
\ n

  if(PHP_SAPI ==='cli-server'){
$ SERVER ['SCRIPT_NAME'] = pathinfo(_ FILE __,PATHINFO_BASENAME);
}
</ code> </ pre>

用法示例:</ p>

  $ php -S localhost:8080 -t public index.php 
</ code> </ pre>
< / DIV>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问