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
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:
[…] 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 […]
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.