I need some help to use Slim Framework. Indeed, I'm a beginner in PHP and I would like to migrate my API using Slim Framework in order to have a clean code. I successed to run Slim Hello Word app on my WAMP server locally, but when I deploy it on my OVH server shared, I have a blank page on route [root]/myproject/hello/test.
The question has already asked but the problem was not exactly the same.
For information I had a 500 internal error locally which was resolved when I've activated rewrite_module on my WAMP server.
I have a SSH access to OVH server but composer is not installed. It seems that mod_rewrite is enabled. WAMP and OVH server use PHP 5.5
This is my folder directory:
Project/
src/
public/
.htaccess
index.php
vendor/
autoload.php
slim/
slim/
Slim/
App.php
...
composer.json
composer.lock
This is my .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
And my index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
Any ideas to resolve it?
Thanks.
try to use this .htaccess config
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /path/to/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Also install monolog https://github.com/Flynsarmy/Slim-Monolog
Related
I'm trying to deploy my laravel (Laravel Framework 7.28.3) to Cpanel, but got a 404 error.
I uploaded my project into /public_html, modified the index.php file to point to the correct files (as below).
I think there must be some mistake in the index.php file but couldn't figure it out.
This is my first time asking my question (after searching for it several times), so hope that I will get the answer!
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
this is my file structure in File Manager:
Deploy laravel application in Cpanel
Setup 1 : - upload file to Cpanel the root directory – not the public_html.
Setup 2 : - Open the that folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html .
Setup 3 : - Navigate to the public_html folder and locate the index.php file. Right click on it and select Code Editor from the menu.
and change this line
require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';
NOTE : - folderName here is in root where you laravel application stay
that's it now all your request will come inside public_html folder index.php and this file will include require __DIR__.'/../folderName/vendor/autoload.php; and run laravel application
Folder structure will look like
/laravel
/public_html/index.php
indside index.php
require __DIR__.'/../laravel/vendor/autoload.php';;
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // here laravel is folder name
You need to make sure your application is in a folder outside of your public_html.
Then you need to make a symbolic link to everything in your public directory inside your application. This symbolic link should be placed in your public_html.
This way your business logic is not available from the outside, only from your own application.
Actually it is advisable to clone your application using git and then install it following the steps in de docs. (https://laravel.com/docs/7.x/installation)
In your public/.htaccess file replaces the code given below..make sure that you uploaded your project in the root public_html folder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
I am following a tutorial to create a http server using Apache, Xampp, slim and php.
I am used to javaScript and Express, so I'm a bit lost here.
Here's how to folder organised:
user.php
<?php
$app->get('/app/test', function (Request $request) {
echo "hello world";
});
?>
app.php
<?php
require __DIR__ . '/../vendor/autoload.php';
use Slim\Factory\AppFactory;
$app = AppFactory::create();
include __DIR__ . '/../app/user.php';
?>
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
index.php
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
?>
In order to test this, I type
http://localhost/friendster/app/test
And this is what I get:
NOTE: The Apache server works correctly:
Any idea what I did wrong?
Create a second .htaccess file in your project root-directory and copy/paste this content:
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
The second .htaccess file is important to run your Slim app in a sub-directory and within your development environment.
After $app = AppFactory::create(); add this line:
$app->setBasePath('/friendster');
Slim 4 - Apache URL rewriting
I have solved the issue by simply starting from scratch and using the Slim Framework 4 Skeleton Application and following the official tutorial to create the first app.
Make sure to install the app using the commands mentioned in the github repo, NOT MANUALLY, otherwise you will face the problem I had here:
So do like this:
composer create-project slim/slim-skeleton [my-app-name]
And then:
cd [my-app-name] composer start
And you will something like this:
The result is this:
I have this code using the slim framework and php with apache2 on linux:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../../vendor/autoload.php';
$app = new \Slim\App;
#doesn't work results in URl not found in browser
$app->get('/hello','sayhi');
#works, when i just type in 'localhost/' into my webbrowser
$app->get('/',function()
{
return 'testing';
});
$app->run();
#functions
function sayhi()
{
echo 'hello world again';
}
.htaccess (same directory as my index.php file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
000-default.conf has the documentroot set to the directory of where my php file and .htaccess file.
When i type in 'localhost/' into my web browser the anonymous function is executed. As that's what it will do if the server recieves '/'
However when i type in 'localhost/hello' it results in URL not found, when it should execute the 'sayhi' function when the server receives this type of url.
Why is it doing this? is there something wrong in my code, as i don't understand how to go about sorting this.
I also tried setting the option to the directory of my php file
AllowOverride All
However this results in a 500 Internal error, when i have this option set. I tried following this guide: 500 internal error guide to no avail.
I have no idea how to sort this, help?
You can tell Apache to redirect all routes to index. Try something like in your vhost or in a .htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA, L]
I think you need to configure apache so that all requests would go into index.php in which your code are located. It seems that when you request localhost/hello your web server tries to find file hello.
I have a very simple Slim app that works fine on my Ubuntu machine but I get an error 500 when I deploy it to a Hostgator shared account using a subdomain.
I suspect the problem lies in my .htaccess file.
The structure of my files is:
project/
├──api/
│ ├── .htaccess
│ ├── services.php
├──src/
├──vendor/
My Slim app (services.php):
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require_once '../src/vendor/autoload.php';
$c = new \Slim\Container();
$c['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
return $c['response']
->withStatus(404)
->withHeader('Content-Type', 'text/html')
->write('Invalid endpoint');
};
};
$app = new \Slim\App($c);
$app->post('/myEndpoint', function (Request $request, Response $response, array $args) {
if($request->getHeader("key") != null && $request->getHeader("key")[0] == "123456789="){
$reqdata = $request->getParsedBody();
$data = $reqdata['data'];
return json_encode('{"data":'.$data.',"response":"OK","errorcode":"0"}');
} else {
return '{"data":"","response":"Access denied","errorcode":"9999"}';
}
});
$app->run();
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . services.php [QSA,L]
</IfModule>
# Use PHP71 as default
AddHandler application/x-httpd-php71 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php71/lib
</IfModule>
I created a subdomain (api.mydomain.com) with project/api/ as document root.
When I make a POST request in my local installation to
127.0.0.1/api/services.php/myEndpoint
works as expected and returns the data. However, when I make the same request to my Hostgator subdomain:
api.mydomain.com/services.php/myEndpoint
I get the error 500.
Note that if I add a simple test.php file to my api/ directory that only prints something, e.g.
<?php
echo "OK"
?>
and I go to api.mydomain.com/test.php, it works fine and prints the string.
What do I need to change or add in my .htaccess for my slim app to work?
I solved it by doing two things:
I moved my project folder to public_html, created again the subdomain and pointed it to the project directory.
I updated my .htaccess file IfModule mod_rewrite.c section. It now looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ services.php [QSA,L]
</IfModule>
The difference with the previous one is I added this line:
RewriteBase /
And this one:
RewriteRule . services.php [QSA,L]
updated to this:
RewriteRule ^ services.php [QSA,L]
I got the .htaccess fixes here:
https://discourse.slimframework.com/t/deploy-slim-project-to-hostgator-using-subdomain/3187/7
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