2015-11-08 134 views
1

首先,我是Silex PHP Framework新手,我正在嘗試爲我的Android應用創建一個RESTApi。Silex路由問題 - 刪除頁面名稱和目錄名稱

我的目錄結構

ABC
----供應商
----網絡
-------- index.php文件
------- -.htaccess
---- composer.json
---- composer.lock

Index.php文件編碼

<?php 

require_once __DIR__.'/../vendor/autoload.php'; 

$app = new Silex\Application(); 

$app->get('/hello/{id}', function ($id) use($app) { 
    return 'Hello '.$app->escape($id); 
}); 

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

$app->run(); 

我的.htaccess文件編碼

RedirectMatch permanent ^/index\.php/(.*) /$1 
<IfModule mod_rewrite.c> 
    Options -MultiViews  
    RewriteEngine On 
    RewriteBase /abc/web/ 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^((?!web/).*)$ web/$1 [NC,L] 
</IfModule> 

當我打這個URL​​

它的工作非常好,並得到響應Hello!

,但是當我打的網址是這樣http://127.0.0.1/abc/web/hello/123

,所以我得到了錯誤這樣

Not Found 

The requested URL /abc/web/hello/123 was not found on this server. 

如果我打這個網址http://127.0.0.1/abc/web/index.php/hello/123所以它的工作很好,我得到了響應確定。 Hello 123

所以,MY QUESTION是我從我的網址刪除頁面名稱index.phpdirweb,我想我的URL看起來像這樣http://127.0.0.1/abc/hello/123

這可能嗎?如何 ?

請幫助,提前致謝。

回答

1

所以發生的是你的重寫規則不是通過Silex前端控制器發送請求(index.php),這是獲得友好路由工作所必需的。我強烈建議您閱讀routing documentation以及example Apache configuration

排序此,下面可能是你的.htaccess工作:

RewriteRule ^((?!web/).*)$ web/index.php/$1 [NC,L] 

我不完全知道什麼是正則表達式^((?!web/).*)$是爲了實現,理想情況下是:

RewriteRule^index.php [QSA,L] 

根據文檔。你的要求雖然可能會決定你擺弄這個,直到它的工作。