Coo
Coo

Reputation: 1882

PHP Silex routes base url

My project is just a test API project on a local server in a subfolder. So my url would be http://localhost/test-project-x/api/. In there is an index.php.

The root route is available through the URL above. But as soon as I go to http://localhost/test-project-x/api/test I get a 404. To get this to work, I need to change the PHP route from $app->get('/test' ... to $app->get('/test-project-x/api/test' ...

I want this to work with /test. But for the life of my I can't seem to figure out / remember how...

test-project-x/api/index.php:

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

$app = new Silex\Application();
$app['debug'] = true;

$app->get('/test', function() use($app) {
    return 'This would be the test route.';
});

$app->get('/', function() use($app) {
    return "This would be the root.";
});

$app->run();

test-project-x/api/.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>

Upvotes: 1

Views: 1574

Answers (1)

mTorres
mTorres

Reputation: 3590

Just use the RewriteBase with the correct folder:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /test-project-x/api/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule>

And there you have, your routes now are on the /test-project-x/api folder.

Upvotes: 1

Related Questions