Slim route with parameter not working - php

I'm trying to setup Slim for me application i've got issue that route with parameters not working.
This is my index.php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// GET route
$app->get('/', function () {
print "hello";
});
$app->get('/books/:id', function ($id) {
print $id;
});
$app->run();
This is the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Request to / works fine, but if I pass /books/1 it returns [404] not found

Man your mod_rewrite is not enable in apache.For enable mod_rewrite follow this.Reference http://www.kingpabel.com/apache-mod-rewrite/
<Directory /var/www/html>
AllowOverride All
</Directory>
//enable mod rewrite
a2enmod rewrite
//restart apache
service apache2 restart

Related

Deploy PHP Slim project to Hostgator using subdomain

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

Slim Framework - Blank page when I deploy

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

Restler 404 error Routes.php:431 with xampp

I am trying to set up a development environment on Windows with xampp 5.6.23.
I have an API I want to set up with Restler 3.0.0 so that I can browse to http://localhost/api/cars/search?term=red to call the search($term) function in the C:\xampp\htdocs\api\cars.php file:
<?php
class cars{
public function search($term) {
// do search...
return $result;
}
}
I also have a C:\xampp\htdocs\api\index.php set up with:
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/luracast/restler/vendor/restler.php';
use Luracast\Restler\Restler;
Defaults::$crossOriginResourceSharing = true;
$r = new Luracast\Restler\Restler();
$r->setCompatibilityMode('2');
$r->setSupportedFormats('JsonFormat', 'XmlFormat', 'JsFormat');
Luracast\Restler\Format\JsonFormat::$unEscapedUnicode = false;
$r->addAPIClass('cars');
$r->addAPIClass('Luracast\\Restler\\Resources');
$r->handle(); //serve the response
and C:\xampp\htdocs\api\.htdocs:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors On
</IfModule>
But if I go to any URL (/ or /cars) I get a 404 error:
<response>
<error>
<code>404</code>
<message>Not Found</message>
</error>
<debug>
<source>Routes.php:431 at route stage</source>
<stages>
<success>get</success>
<failure>route</failure>
<failure>negotiate</failure>
<failure>message</failure>
</stages>
</debug>
</response>
I have tried multiple answers from SO but none have worked in my instance. I have uncommented LoadModule rewrite_module modules/mod_rewrite.so and set AllowOverride All everywhere I could find it in httpd.conf. I have also tried moving my everything to htdocs instead of a sub folder but still get the same result.
Since you don't have an index() function but only a search() function in your class cars, you need to access the URL /cars/search/term.
Or are you trying to achieve something else?

unable to remove index.php from laravel url ?? in ubuntu 14.04

there
I just created a new project in laravel 5.2. then I had successfully removed public/index.php from url of my project in windows it works fine.
But While I copied the same project in ubuntu inside var/www/html as /var/www/html/myproject. And enabled apache2 rewrite_mode. But it no work without putting index.php in url. It works without public only.
http:://localhost/myproject/index.php/cms (it works fine) BUT
http:://localhost/myproject/cms(it does not works)??? .Any suggestion????
my code is as
route.php
define( "PREFIX", "cms" );
Route::get(PREFIX . '/login', 'Auth\AuthController#getLogin');
it fine
also I already cut all files from public folder to root directory and edit index.php file as
require __DIR__.'/../bootstrap/autoload.php';
By
require __DIR__.'/bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../bootstrap/app.php';
by
$app = require_once __DIR__.'/bootstrap/app.php';
.htaccess as
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I ran into this and foud a solution on http://laravel.io.
On Apache 2.4.7 the apache2.conf is set to not allow .htaccess override by setting AllowOverride None:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
You can change this in apache2.conf, but this is not recommended. Instead do this override in the VirtualHosts file for your site ie. sites-available/yoursite.com.conf. Something like this:
<Directory /var/www/html/yoursite.com/laravelappinstall/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Make sure that you have enabled the rewrite module ie. sudo a2enmod rewrite. Restart your web server ie. sudo service apache2 restart.
Source: http://laravel.io/forum/09-15-2015-removing-indexphp-from-url-laravel-5116
•Go to mainproject/public>>
a. .htacess
b. favicon.ico
c. index.php
d. robots.txt
e. web.config
1.cut these 5 files from public folder,and then paste on the main project folder that’s means out side of public folder… mainproject/files
2.Next after paste ,open index.php ,modify
•require __DIR__.'/…/bootstrap/autoload.php'; to
•require __DIR__.'/bootstrap/autoload.php';
modify
$app = require_once DIR.'/../bootstrap/app.php'; to
$app = require_once DIR.'/bootstrap/app.php';

Error 500 with Slim on "subfolder"

I have an index.php file in /api folder on my server. And I'm beginning with Slim and I'm trying to make a REST api.
So in my index.php I've got this:
$app->get('/', function() use ($db) {
echo "Get";
});
$app->post('/', function() use ($db) {
echo "Post";
});
$app->get('/test', function() use ($db) {
echo "Test";
});
And in my .htaccess file, as it was downloaded:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
If I make a GET call to ../api/ I've got "Get", the post call works fine too, but if I make .../api/test call I've got a 500 Internal Server error.
What am I doing wrong?
Thank you!

Categories