2013-07-26 152 views
1

documentation之後,Silex允許通過URL傳入「slugs」以供在您的代碼中使用。Silex slugs導致404頁面未找到

在下面的示例:

$app = new Silex\Application(); 

$app->get('/', function() { 
    return 'HAI'; 
}); 

然而,下面給出一個404未找到:

$app = new Silex\Application(); 

$app->get('/{slug}', function ($slug) { 
    return 'HAI' . $slug; 
}); 

我怎樣才能解決這個404?

在情況下,它的任何相關性,這是我的Apache虛擬主機:

<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 

回答

3

事實證明,這是一個Apache的問題。假設您可以使用.htaccess文件,虛擬主機。你實際上需要使用

的.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指令),並擺脫的htaccess的。

只要我添加了.htaccess,我的第二個示例中的slu worked就工作了。

+0

很確定你可以從你的虛擬主機配置中刪除DirectoryIndex index.php – cheesemacfly

+0

嗨@cheesemacfly,如果你想複製這個答案,併爲你自己添加它,當你發現它時,隨意,我會獎勵你upvote &答案:) – Jimbo

+0

沒有必要,你在這裏完成了這項工作! :) – cheesemacfly