UPDATE:

Aug-1-2016

From B7th:

After trying to find workaround solutions, I found this to work perfectly fine and without having to deal with too many things:

`./.htaccess`

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ public/$1 [L]

`./public/.htaccess`

RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ../index.php/$1 [L,QSA]

 

Your index.php is to be kept outside the public/ folder, is needed if you are not at the root, and links do not need a starting slash.

 

OLD:

I was recently playing around with Mini2 – “An extremely simple PHP barebone / skeleton application built on top of the wonderful Slim router / micro framework.” I love how minimal and easy-to-learn the framework is.

Maybe for the first time I’m able to understand the whole MVC (model-view-controller) concept. However, I ran into a small problem when trying to make my app run on a subfolder.

Because I was still experimenting and hunting for a framework, I’d put eveything I find in a phptests subfolder on localhost.

My root URL looks like http://localhost/phptest/mini2/ and in order to run it, I’d navigate to http://localhost/phptests/mini2/public/.

Well, that works fine but I didn’t like that public folder and wanted to access my app by going to just the root – /mini2.

After a day of trying different VirutalHost and .htaccess solutions, I found out that the problem was in Slim itself.

The following is the solution that worked  for me:

C:/xampp/htdocs/phptests/mini2/.htaccess

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

 

C:/xampp/htdocs/phptests/mini2/public/.htaccess (it’s the default)

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

 

C:/xampp5/htdocs/phptests/mini2/vendor/slim/slim/Slim/Environment.php

Open the following file and look for “Virual path” (in my case line 143) Replace the first line (or comment it out) with the first line below:

// Virtual path
//(You can remove this line) $env['PATH_INFO'] = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path 
$env['PATH_INFO'] = str_replace(str_replace('/public', "", dirname($_SERVER['PHP_SELF'])), '', "/".$requestUri); //removes '/public'
$env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string
$env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash

If you've done eveything correctly you should be able to access your slim or mini2 app by going to it's root folder. In my case localhost/phptests/mini2

mini2-htacess-public-virtualhost-404

 

But we're not done yet. :(

Because Slim and Twig uses slash "/" in front of all URLs it would always return to http://localhost.

That's why it's probably best to use absolute paths, based on a predeifned variable. 

I added a base variable to the view object and the app - here's what I have in my index.php - (change starts from line 15 to the end)

base = $base;
$app->hook('slim.before', function () use($app) 
{ 
$app->view->setData(array('base' => $app->base)); 
});

 After that you'll have to edit all your view files (.twig) to use the base variable like this: 

Before: 

/songs/updatesong" method="POST"> 

After:  

{{ base }}songs/updatesong" method="POST"> 

Same applies for all your a href links: Here's what my _base.twig looks like:






MINI 2


















{% block content %} {% endblock %}



I hope this helps!

Handle errors:

If you try to access your app before fixing the Environment.php, you'll proably get an Error 404 or Error 500. I got both at some point. The Server error 500 is when you create a htaccess loop. The 404 when Slim can't handle your routes. 

The loop error will throw error 500 but if you check your c:/xampp/apache/logs/error.log it will look like this:

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

This means that you messed up the htaccess files (or the VirualHost)

I was also trying to use RewriteBase and I got:

 C:/xampp/htdocs/phptests/mini2/.htaccess: RewriteBase: argument is not a valid URL

Later, I found out that RewriteBase is not ment for the purpose or hiding or removing a subfolder from a URL (from localhost/app/public/ to just localhost/app/)

The Evironment.php bug was found on stackoverflow: http://stackoverflow.com/questions/15443504/routing-issues-with-slim-framework-running-on-appfog

2 Comments

  • […] example, Slim uses $_SERVER['PHP_SELF'] which would break routing by forcing /public into the url. One dirty way to solve this involves changing a file in the vendor/slim/slim folder, which should be a relatively big […]

  • B7th says:

    After trying to find workaround solutions, I found this to work perfectly fine and without having to deal with too many things:

    ./.htaccess
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule ^(.*)$ public/$1 [L]

    ./public/.htaccess
    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ ../index.php/$1 [L,QSA]

    Your index.php is to be kept outside the public/ folder, is needed if you are not at the root, and links do not need a starting slash.

Leave a Reply