Deploying Slim to an existing website

I have got a working website where I wish to replace my plain php json encoded data with Slim 4. Using Wampserver I am able to set-up the Slim 4 framework and it is working IF the website is placed on a different folder (localhost/project) and the slim 4 framework is virtually hosted as “slim” for example. I could combine the files of the website and the slim framework and the website will work with “localhost/slim” where slim is the folder where the slim framework is also stored.

Now I am not sure how to make this work online. Locally, I virtually hosted the public folder of the slim framework hence it worked. When hosted online, how would I be able to set it up so that I could access my APIs? And how would I combine my existing website with that of the slim framework? I added an .htaccess file in the root (public) of my website but it generates a “404 Not Found” error when I am accessing it.

I am very new to using a REST API framework so please be kind to me. :slight_smile: TIA

EDIT: when I place the .htaccess in the root folder of my website I am able to access my API’s but not the index.php of my website. Do I need to modify the .htaccess file or should I change the structure of my website?

Hello @dee-u You have to set the correct basePath depending on your environment (development, prod, etc).This basepath middleware works for me: basepath

It is just working fine in my development, my problem is when it is hosted online. Adding .htaccess in the root directory prevents me from accessing the index.php, while if I remove it then the APIs are not accessible. How should I use basePath to remedy my problem?

You should not allow direct access from “the web” to your public/index.php file. The public/index.php file should be “invisible” and is never part of the url. That’s why you should always add at the .htaccess file to your public/ directory (for development AND production).

Yes I know but the problem is it makes my site inaccessible, I cannot access index.php anymore.

directorystructure

Here’s the screenshot of my current structure as of now. As you can see I have there the “skeleton” of my Slim framework (vendor,public and src). With .htaccess enabled (when I remove the x) I can access my APIs but unable to access my index.php file. What is the workaround for this setup?

Ok I know whats the problem. The current directory structure will not work in real life.

In a web application / website, it is important to distinguish between the public and non-public areas.

The public/ directory serves your application and will therefore also be directly accessible by all browsers, search engines and API clients. All other folders are not public and must not be accessible online. This can be done by defining the public folder in Apache as DocumentRoot of your app / website.

I have written about this this topic in this Slim 4 Turorial.

The “solution” is quite simple:

  1. Move the file index.php file into public/ directory (maybe fix some paths to the autoloader)
  2. Move your public “assets” (js, css etc) directory into the public/ directory too
  3. In your prod env: Define the public/ path as your DocumentRoot
  4. Create a file: public/.htaccess with this content:
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
  1. Create a second .htaccess file above the public/ directory with this content:
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

The second .htaccess file is only importand for your local development environment, but can be deployed too.

1 Like

Thank you so much for your help, in a way it appears I don’t need the .htaccess in my root folder, it now works using the setBasePath you mentioned, I followed the tutorial here and it worked:

https://akrabat.com/running-slim-4-in-a-subdirectory/

1 Like

I spoke too soon, I only realize now that it will not really work without the .htaccess in my root directory, I happen to have missed updating my local code which points to the virtual host I created. I am in a dilemma, will I be able to deploy the Slim Application to an existing website at all?

@odan it appears in your latest reply that it is not possible to deploy Slim Application to an existing website which has its own index.php file?

With Slim it should be possible. But I don’t have enough contextual information from here. I need full access to the source code to see what’s possible. You can consult me if you need my further assistance.

Upon investigation, my APIs are accessible without the .htaccess if I’ll access them in the directory they the index.php is saved, for instance like “www.website.com/API”, but the API there is a subdirectory and I could access the APIs like “www.website.com/API/API1”, “www.website.com/API/API2”, etc. For now that should suffice but I am not sure if it is the correct remedy in this case. It is just sad that there doesn’t seem to be enough support for those who want to use the framework, no one here aside from you has replied to this thread and thank you so much for that. I may try other frameworks with more support if Slim has no active community of active supporters to reply to inquiries from new users.