【问题标题】:Silex: reset the root route when the app is not at the webroot levelSilex:当应用程序不在 webroot 级别时重置根路由
【发布时间】:2017-01-05 03:51:09
【问题描述】:

我正在玩 Silex,试图在共享网络托管中将其用作 RESTful json api。主机有 Apache Web 服务器。我希望 Silex 应用程序位于我暂时称为 experiments/api 的文件夹中,因此该应用程序与 webroot 处于不同的级别。根据documentation,我放在Silex 应用文件夹中的.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 的根路由以包含路径的常量部分?我浏览了文档,但找不到答案。

【问题讨论】:

    标签: .htaccess silex


    【解决方案1】:

    您可以使用mount feature

    这是一个快速而肮脏的例子:

    <?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);
    
    //...
    

    【讨论】:

    • 谢谢;这就是我最终所做的。
    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2017-02-20
    相关资源
    最近更新 更多