4
votes

I am new to slim framework. Currently on my existing webserver which is on centos 7 I have other php based application running. So currently my directory structure is like this.

var/www/html
    phpapp1
    phpapp2
    apislim

The folder for the apislim I created was for slim framework. Below are the exact steps I did was 1. composer create-project slim/slim-skeleton
2. I rename the slim-skeleton folder to apislim 3. I make sure the owner is apache chown -R apache:apache apislim 4. In the httpd.conf I ensure this AllowOverride is enabled to be All

<Directory "/var/www">
    AllowOverride All
    # Allow open access:
    Require all granted
    Options -Indexes
</Directory>

Also below I enabled All

<Directory "/var/www/html">   
    Options -Indexes -FollowSymLinks
    AllowOverride All
</Directory>
  1. In the apislim/public folder I have this .htaccess file as below.

    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.)::\2$ RewriteRule ^(.) - [E=BASE:%1]

    RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

  2. The I have also the index.php file in the public folder which also links up to the src for the routes and the main folder apislim I created another .htaccess file and added this.

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

The issue now I want my existing application to work along with this rest api which is based on slim framework. So when I go to this link

http://*.*.*.*/apislim/
http://*.*.*.*/apislim/public/
http://*.*.*.*/apislim/public/index.php

Neither of it works all are giving me 403 forbidden. Then I check the error log it show error regarding FollowSymLinks So I added Options -Indexes +FollowSymLinks Into

So next error I get now is 500 interval server error.

1
Check your Apache error log - it should give you some hint as to why the 500 error is occurring. Also check and make sure you have a recent enough version of PHP - the most recent version of Slim requires PHP 5.5 or later, and CentOS 7 ships with PHP 5.4.Virtually Nick
@VirtuallyNick I have all the latest version php 7 centos 7. Actually is it possible to run slim framework with existing application?user5313398
I'm not sure what you mean by "run slim framework with existing application?" It can certainly co-exist with other items on the same server, but the Apache configuration would need to be done correctly such that the RewriteRule entries from one do not interfere with the other, and vice-versa.Virtually Nick
Can you try this two .htaccess files? file1, file2odan
@VirtuallyNick yes I wanted to confirm can it can co-exist. Thank youuser5313398

1 Answers

8
votes

The following steps are necessary for your Slim 3 application to work within subdirectories.

Directory structure:

  • public/ Web server files (the DocumentRoot)
    • .htaccess Apache redirect rules for the front controller
    • index.php The front controller
  • .htaccess Internal redirect to the public/ directory

The content of the file: .htaccess:

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

The content of the file: public/.htaccess:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L] 

Add this container entry to the file: dependencies.php:

// Activating routes in a subfolder
$container['environment'] = function () {
    $scriptName = $_SERVER['SCRIPT_NAME'];
    $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);
    return new Slim\Http\Environment($_SERVER);
};

Edit: In Slim 4 you should use the $app->setBasePath($basePath); method. More details