.htaccess Silex:当应用程序不在webroot级别时重置根路由

.htaccess Silex:当应用程序不在webroot级别时重置根路由,.htaccess,silex,.htaccess,Silex,我正在玩Silex,试图在共享的web主机上将它用作RESTful json api。主机具有Apache web服务器。我希望Silex应用程序位于我暂时称之为experiments/api的文件夹中,因此该应用程序的级别与webroot不同。根据,我放在Silex app文件夹中的.htaccess文件如下所示: RewriteEngine On RewriteBase /experiments/api RewriteCond %{REQUEST_FILENAME} !-f RewriteC

我正在玩Silex,试图在共享的web主机上将它用作RESTful json api。主机具有Apache web服务器。我希望Silex应用程序位于我暂时称之为
experiments/api
的文件夹中,因此该应用程序的级别与webroot不同。根据,我放在Silex app文件夹中的
.htaccess
文件如下所示:

RewriteEngine On
RewriteBase /experiments/api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ src/index.php [QSA,L]
(这意味着应用程序位于/experiments/api文件夹中,主控制器文件位于src文件夹中,名为index.php)

这就完成了任务(即Silex应用程序接收到对
/experiments/api/
的请求),但不便之处在于,应用程序现在看到路径名的前缀
/experiments/api/

比如说。当我向
/experiments/api/hello
发送GET请求时,我希望应用程序忽略
/experiments/api
部分,并且只匹配
/hello
路径。但目前该应用程序试图匹配整个
/experiments/api/hello
路径

是否有方法重置Silex的根路由以包括路径的常量部分?我查看了文档,但找不到答案。

您可以使用

下面是一个简单的例子:

<?php
// when you define your controllers, instead of using the $app instance 
// use an instance of a controllers_factory service

$app_routes = $app['controllers_factory'];
$app_routes->get('/', function(Application $app) {
    return "this is the homepage";
})
->bind('home');

$app_routes->get('/somewhere/{someparameter}', function($someparameter) use ($app) {
    return "this is /somewhere/" . $someparameter;
})
->bind('somewhere');

// notice the lack of / at the end of /experiments/api
$app->mount('/experiments/api', $app_routes);

//...

谢谢你;这就是我最终要做的。