问题
I've trying to build an profanity filtering API by using slim. Here is the link of that api.
Link : http://www.employeeexperts.com/Profanity/index.php/rest/check/hello
At the end of the link, i'm appending the word 'hello' for profanity checking. Till now, it's working properly. But, the moment i added any dot(.) at the end of word (ex: hello..) , Slim redirecting the control to index page.
My GET router code is like this..
$app->get('/service/:method/:str', function ($method, $str) use ($app) {
// Internal codes goes here
});
Can any one help me fig out how i can stop this.
Regards
回答1:
It seems the default GET router doesn't let you have URLs ending in a dot. If you have any character after the dot, the page will function normally. As an alternative you could try a custom route condition as shown here: http://docs.slimframework.com/#Route-Conditions
Example for your case:
<?php
$app = new \Slim\Slim();
$app->get('/service/:method/:str', function ($year) {
echo "You are viewing archives from $year";
})->conditions(array('str' => '([a-z]+)', 'str' => '(.*)'));
If you are planing on processing complex data with your API (like complete articles or similar) I would suggest transferring the data via POST. It saves you a lot of headaches.
回答2:
I have this problem too, this is my solution:
$app->get('/service/+args', function ($args) use ($app) {
$arguments = str_replace("/service/", $app->request->getPathInfo());
$args = explode('/', $arguments);
var_dump($args);
});
When I access to URL /service/foo/bar
, the $args
will be like this:
array(
'foo',
'bar',
)
Also, when I access to URL /service/foo/bar/baz/qux
, the $args
will be like this:
array(
'foo',
'bar',
'baz',
'qux',
)
来源:https://stackoverflow.com/questions/18362793/php-slim-not-accepting-dot-in-uri-argument