Issues Hosting PHP app with Slim Framework on VPS - php

I have a PHP application that is using Slim framework for APIs. Everything is working fine on localhost...however... not on the VPS..I am using Digital Ocean.
Here is my project structure.... Just wanted to ensure whether it is right.
ROOT DIRECTORY
- app
-- admin - index.php (Main App Homepage)
- .htaccess
- Other CSS & javaScript
API (This is Slim Project)
-- includes
-- lib
-- v1
.htaccess (htaccess of Slim)
-- index.html
MAIN ISSUE:
On running the app... say login page...it makes a request to the Slim ...the api responds back with the json...but it shows in the browser rather than showing the html formatted response.
Not sure what is missing.
.htaccess for Slim
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
.htaccess for main app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>

Did you change the content-type to application/json ?
Check this, Slim has a simple way to return a json, this way :
$data = array('name' => 'Bob', 'age' => 40);
$newResponse = $oldResponse->withJson($data);

Related

How can I use an .htaccess for specific path to listen child path

ReactJS application sits inside page path - www.example.com/some-path
Reactjs App is part of page and not cover full page - so we cant add extra subdirectory and add reactjs there.
How to change htaccess to make situation where all paths after some-path - (some-path/foo or some-path/foo1) listening my reactjs app inside some-path
At the moment it works only if I visit some-path, but if I try to open some-path/foo then it takes server path that is empty and send empty page 404.
I have deployed multiple working single-page applications (reactJS) into main route of server and used htacces like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
There is no any some-path folder's inside server because it is wordpress application where we hold our react app.
Can anyone help?
Found a working solution
I made its own directory for (some-path)
Inside that path added:
index.php - with wordpress (header, footer) - middle of that added my reactjs application
.htaccess to listen that reactjs app - now all routes runs well

htaccess - Redirect parent folder PHP script

I have this web api project which is developed by other company. The file structure is:
/project
---/app
---/ApiEndpoint.php
---/public
---/index.php
The DocumentRoot is pointing to /project/public. The index.php is working (http://myapi.com/), however when I try to browse into the api endpoint http://myapi.com/api/endpoint I got 404 error.
How do I configure the .htaccess to rewrite this condition?
/project/public/.htaccess config
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
The current configuration is there to make sure every request go to index.php (that is probably dispatching requests in some way) except for the files that actually are in the public directory (probably needed for static files like images, and such).
If you wrote a php yourself to be directly called by http://myapi.com/api/endpoint, you should put it in /project/app/public/api/endpoint/index.php.
BUT I suspect you should study that application more and understand the current dispatching method, before doing that.

Slim Framework /public folder redirect

I am using Slim Framework v3. I've set up API and its working smoothly if I access http://localhost:8080/slimapp/public
I have default directory structure. My Sample API endpoint is http://localhost:8080/slimapp/public/cards which returns JSON response of my cards
How Could I change the public folder to the domain, So I would be able to access my cards endpoint with http://localhost:8080/slimapp/cards?
You can simply rewrite the requests with
RewriteCond %{REQUEST_URI} !^/slimapp/public
RewriteRule ^slimapp/(.*)$ /slimapp/public/$1 [L]
This will serve the appropriate public folder, without redirecting the client. The RewriteCond is needed to avoid a redirect loop.
You can also edit Slim's default .htaccess file as:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [QSA,L]
Note: .htaccess file must be located in server root

Slim Framework always return void 200OK

I have a web poject in a GoDaddy shared host. Is a webpage with an APIrest made with Slim Framework. This is the structure:
public_html/
- index.html (main webpage)
- css, js, ...
test/
include/
- database handler, connection, etc
rest/
- index.php (API)
- .htaccess
Slim/ (framework files)
The .htaccess is the original:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
The PHP version is the same on local server and the host (5.4).
The problem is that in local server works perfect, but when I put it in the host not. If I try the example:
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
I get a status 200 OK with void body. Any idea where is the problem? Thanks
Edit:
When change .htacces like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I always get status 404 not found

How to place zend framework application in a wordpress website

Recently I created an application (sales portal) in PHP for a small company. The company had a website developed in word press. Since I hadn't worked with word press the way I embedded my application in the website is I simply created a sub directory on the host and uploaded my application there. E.g:
domainname.com - their website
domainname.com/portal - is where my application is placed (the 'index.php' file).
Since a month I am learning Zend Framework 1.8 and I want to rewrite the portal in Zend framework since I wrote the portal from scratch and its core is not as secure as it would be if it implements the Zend framework.
My question is can I include the Zend framework application into the wordpress website the way I did it with 'from-scratch' application, by creating a sub directory on the host and upload the application there? And if yes how should I configure the Zend application so that it recognizes 'domainname.com/portal' as the domain name (as home directory).
The problem that I face right now is that when I type http://www.domainname.com/portal/sales it returns 404 because there is not such directory on the server. Of course what I mean with the above mentioned link (domainname.com/portal/sales) is:
site: domainname.com/portal
controller: sales
action: index
I tried 'domainname.com/portal/index.php/sales' but when someone opens the portal with this link 'domainname.com/portal/' the next linked that is clicked (e.g. domainname.com/portal/sales) shows 404.
(note: the website http://www.domainname.com should be accessible also)
Thanks in advance.
I believe that wordpress ues a .htaccess to redirect all URLs to its index.php.
This is the .htaccess I pulled out of a test installation:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
In your case, you would need to modify the .htaccess file so that it does not care about what's in http://domainname.com/portal.
In your case, your .htaccess file could look at this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} "/portal/"
RewriteRule (.*) $1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Basically, what this does is that it ignores the subfolder 'portal' and doesn't process it with any of the normal rules.
However, there might be a down side: If future versions of wordpress updates the .htaccess file, then you will need to make those changes to the file again.
[C]an I embed the Zend framework application into the wordpress website the way I did it with 'from-scratch' application, by creating a sub directory on the host and uploading the application there?
Yes you can.
[H]ow should I configure the Zend application so that it recognizes domainname.com/portal as the domain name (home directory)[?]
Sure, please see Base Url and Subdirectories­Docs how you need to configure your request/routing for your Zend Framework based application.

Categories