1

I have the following structure:

/home/
    alvaro/
         public_html/
             Code Playground/
                 SlimTest/
                     logs/
                     public/
                     src/
                     templates/
                     vendor/
                     ...

http://example.com points to /home/alvaro/public_html, thus http://example.com/Code Playground/SlimTest points to /home/alvaro/public_html/Code Playground/SlimTest, directory on which I've placed this .htaccess file:

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

In short, loading http://example.com/Code Playground/SlimTest in the browser executes my /home/alvaro/public_html/Code Playground/SlimTest/public/index.php Slim/3.9.0 (slim/slim-skeleton) entry point.

My problem is that I can't get routes right. I've tried every conceivable combination:

$app->get('/Code Playground/SlimTest', …);
$app->get('/Code Playground/SlimTest/', …);
$app->get('/Code Playground/SlimTest/public', …);
$app->get('/', …);
// …

... but I always get the 404 Not Found page delivered by Slim's NotFound class.

  1. Is there a way to get base path upon route definition so I can prepend it in get()?

  2. If not, what would a hard-coded path look like?

  3. Should I look for a fix in Apache, such as Alias?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    That's because Slim has no clue it's being run from the subdirectory. I would have expected that the latter route "/" worked but alas. Have you tried any of these suggestions: https://github.com/slimphp/Slim/issues/1529 ? – Edwin Nov 16 '17 at 11:03
  • @SketchyCoder After playing around with the information in the GitHub ticket and debugging `\Slim\Http\Uri::createFromEnvironment()` I have a strong suspicion that the feature is built-in and should work out of the box in current version, but it doesn't work for me because of a bug. Slim compares `REQUEST_URI` (URL encoded) with `SCRIPT_NAME` (not encoded) and my actual directory name has a space (`%20` != ` `). Sorry, when redacting names for brevity I never thought spaces would matter. – Álvaro González Nov 16 '17 at 16:38
  • No worries! Did you manage to solve it? – Edwin Nov 16 '17 at 16:53
  • 1
    @SketchyCoder I've found a couple of ugly workarounds (that mostly involve adding `%20` here and there) but this is getting personal so I'll try to find a proper fix when I have some time. Meanwhile I've edited the question to fix redacted names. – Álvaro González Nov 17 '17 at 09:05
  • I officially give up. Every workaround fixes one thing and breaks another. I'll just create separate sites or will use the PHP web server. – Álvaro González Nov 19 '17 at 16:46
  • Sorry to hear that! Good luck with the alternative solution – Edwin Nov 20 '17 at 07:06
  • It is bad practice to put all your web application source code inside public directory. – Zamrony P. Juhara Nov 23 '17 at 00:24
  • @ZamronyP.Juhara Absolutely! But the public directory here is `/home/alvaro/public_html/Code Playground/SlimTest/public/`. Apache doesn't serve files from `/home/alvaro/public_html/Code Playground/SlimTest/logs` or `/home/alvaro/public_html/Code Playground/SlimTest/src`; not even `/home/alvaro/public_html/Code Playground/SlimTest`. – Álvaro González Nov 23 '17 at 08:17

0 Answers0