0
votes

I am playing with Silex, trying to use it as a RESTful json api at a shared web hosting. The host has Apache web server. I would like the Silex app to sit in the folder which I tentatively called experiments/api, so the app becomes at a different level than the webroot. As per the documentation, the .htaccess file that I placed in the Silex app folder looks like this:

RewriteEngine On
RewriteBase /experiments/api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ src/index.php [QSA,L]

(meaning that the app sits in the /experiments/api folder, and the main controller file is in src folder and is called index.php)

This gets the job done (i.e. the requests to /experiments/api/ are picked up by the Silex app), but the inconvenience is that the app now sees this /experiments/api/ prefix of the pathname.

For example. When I send a GET request to /experiments/api/hello I want the app to ignore the /experiments/api part, and to match only the /hello route. But currently the app tries to match this whole /experiments/api/hello path.

Is there a way to reset the root route for Silex to include the constant part of the path? I looked through the docs, but couldn’t find the answer.

1

1 Answers

1
votes

You could use the mount feature.

Here's a quick and dirty example:

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

//...