Search code examples
phproutesproduction-environmentslim

Production server Slim Framework routing


This is my first time working with Slim Framework, so far I really like the look and feel of the application. Makes it almost like an express API server.

I have built a small server on my local machine, which works perfectly fine, working with a virtual host.

It has something to do with the virtual host I have set up locally, but I tried to circumvene this with .htaccess files.

On the production server it does work if I enter http://www.domain.com/public/resource.

My directory structure is the following:

public/
    index.php
    .htaccess
src/
.htaccess

The .htaccess in the root folder looks like this:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^$ public/    [L]
   RewriteRule (.*) public/$1 [L]
</IfModule>

The .htaccess in the public folder looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

I will also post a part of my index.php and show where I've logged so far.

$app = new Slim\Slim();

$env = $app->environment();

switch($env['REMOTE_ADDR']) {
    case '127.0.0.1':
        define('ENVIRONMENT', 'local');
        break;
    default:
        define('ENVIRONMENT', 'production');
        break;
}

// Here a log works when you go to http://www.domain.com

// Get
$app->get('/:resource(/(:id)(/))', function($resource, $id = null) {

    // Here it works on local machine on production there's no possible way to get here
    // var_dump('foobar');die; will respond nothing to cURL -i -X GET http://www.domain.com/resource

    // The request will return a 404 error

    $resource = \App\Resource::load($resource);
    if ($resource === null) {
        \App\Resource::response(\App\Resource::STATUS_NOT_FOUND);
    } else {
        $resource->get($id);
    }
});

Solution

  • After some research it turns out, that Slim doesn't now how to cope with this problem yet.

    As adressed here: https://github.com/codeguy/Slim/issues/521 this will not change in the near future. So I fixed the problem with a virtual host.