在documentation之后,Silex允许通过URL传递“slugs”以便在您的代码中使用。
以下示例有效:
$app = new Silex\Application();
$app->get('/', function () {
return 'HAI';
});
但是,下面给出了404 Not Found:
$app = new Silex\Application();
$app->get('/{slug}', function ($slug) {
return 'HAI' . $slug;
});
如何修复此404?
如果它有任何相关性,这是我的Apache Vhost:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
...和我的目录结构:
/src
|-- bootstrap.php
/tests
/vendor
/web
|-- index.php
答案 0 :(得分:3)
事实证明这是一个Apache问题。假设您可以使用.htaccess文件,或 vhost。您实际上需要使用两者。
<强> htaccess的:强>
FallbackResource /index.php
注意:如果使用Apache 2.2.16 or higher,则只能使用FallbackResource。
<强>虚拟主机强>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
另一种方法是将.htaccess
文件(FallbackResource
指令)的内容放在vhost本身中,并删除htaccess。
一旦我添加.htaccess,我的第二个例子中的slug就可以了。