关于Silex路由的一个简单疑问。我是Silex的新手,基本上我正在学习它,因为一切都进展顺利,问题是(??)-
这是我的index.php->
require_once __DIR__.'/../vendor/autoload.php';
$app = new SilexApplication();
$app->register(new SilexProviderUrlGeneratorServiceProvider());
use SymfonyComponentHttpFoundationResponse;
$app->get('/', function() {
return 'Hello World!';
});
$app->get('/hello', function() {
return 'Hello From HELLO!';
});
$app->error(function (Exception $e, $code) use($app) {
switch ($code) {
case 404:
$message = "Oooops Not Found";
break;
default:
$message = $app['twig']->render('error500.html.twig');
}
return new Response($message, $code);
});
$app->run();
问题是关于路由,只要我们试图访问主页,它就会起作用,或者/hello(http://mysite.dev或http://mysite.dev/hello)。但是如果我试图访问一个不存在的链接,像这样-http://mysite.dev/hello/blah它会像预期的那样返回404页面($app->error()),但如果我删除/blah部分并尝试在浏览器中再次输入(http://mysite.dev/hello)-我仍然得到404,要访问该网站,我必须一直回到root(即http://mysite.dev)。我不知道我是不是缺少了一些配置或其他什么,或者可能是一个愚蠢的配置,但请允许我是编码的初学者。
这里有一个很好的例子:-转到https://getcomposer.org/doc/00-intro.md它将引导您进入composer文档入门页面,如果您在这个url的末尾添加一些内容,如下所示https://getcomposer.org/doc/00-intro.md/blah-它会给你"对不起,找不到你要找的页面。"错误,好的,如果你想回到开始页面,如果你试图删除/blah并再次输入,你不可能仍然得到相同的错误页面,有人能解释一下吗。
这里也是http://silex.sensiolabs.org/doc/
提前谢谢。
路由不需要尾部的正斜杠。Thsi是预期行为,已详细讨论:https://github.com/silexphp/Silex/issues/149
建议您定义一个冗余路径,其中包含斜线:
$app->get('/hello', function() {
return 'Hello From HELLO!';
});
$app->get('/hello/', function() {
return 'Hello From HELLO!';
});
由于第二个参数是回调,这可能是:
$hello_handler = function() {
return 'Hello From HELLO!';
};
//or
$hello_handler = array($object, 'handler_method');
$app->get('/hello', $hello_handler);
$app->get('/hello/', $hello_handler);
或者你想出的任何简单易用的方法。
原因是/index.html
与/index.html/
不同。