PHP Slim Framework Routing does only work locally - php

I have built something small using the Slim Framework for routing. Everything worked perfectly locally. I have rented a Droplet now and use a LAMP stack on Ubuntu 18.04. My App is located in the location /var/www/src/public.
I have already added this into the apache.conf:
<Directory /var/www/src/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
My 000-default.conf already has the correct route setted.
My .htaccess.txt is located in /public with my index.php which contains:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
I have already enabled mod_rewrite for Apache.
When I call the servers IP address the main page is loaded successfully with Slim where the routing is set like this:
$app->get('/', function (Request $request, Response $response) {
return $this->renderer->render($response, "/index.php");
});
So Slim seems to be rendering my index.php which is in the directory /var/www/views correctly. This path is defined in the container like this:
$container['renderer'] = new PhpRenderer("../../views");
The problem starts when I try to reach for example the site ip_address/player even though it is correctly routed and was functioning locally. Trying to reach it gives me this in the browser:
Not Found
The requested URL /player was not found on this server.
I have googled for several hours trying different solutions but I just can't get it to work. Any help would be greatly appreciated.

Should not it be .htaccess instead of .htaccess.txt. What kind of OS you're at?

Related

Laravel not good work on localhost

I have a code (who works on remote server), but he don't want work on my VM linux (ubuntu 16).
Issues are varied :
CSS isn't read
#import and #expand don't work
Laravel routes don't work
Apache 2 rewrite_module is ok (classical solve for Laravel routes issue)
PHP 7 is ok (phpinfo is good), Laravel is ok (access to database via Eloquent is good).
I precise that my html code is generate via php artisan with blade.
I suppose that I forgot a step in my configuration but I don't see what...
Edit :
Change on Apache's config DocumentRoot from root of application to /public solve the CSS issue.
a valid .htaccess is now ok, without change.
So, now, my major issue is to find why
https://www.xxxxx.vvv/api/v3/docs works and https://localhost/api/v3/docs doesn't.
Given that https//www.xxxxx.vvv/ and http://localhost/ work in the same way.
versions : php 7.0.3
Laravel 5.0.4
(and I don't want update)
As you said, .htaccess file missed. So put it in public directory with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

simple php Slim project errors

I've created a simple Slim microframework project using PHPStorm 8 on Windows 8.1 with WAMP Server installed. All WAMP Server settings are set by default.
I created a new project called pr1
I used 'Init Composer...' and then added some dependencies like slim/slim, slim/views, twig/twig.
Then I tried to create a simple application just like a given example on main Slim page:
I've created file called index.php in my project folder
index.php
code:
require 'app.php';
Then I've created file app.php
code
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
After this I tried to run my project in Chrome and there an error 404 occured.
Then I tried to pass my name through url: http://localhost:63342/pr1/wade and there was PHPStorm error.
After this steps I've tried to close PHPStorm and my project in browser:
and it seemed like there's a typical Slim 404 error,but when I tried to pass my name through url again it gave me this error:
you need to include index.php for example http://localhost/pr1/index.php/test
to get rid of index.php use .htaccess or something equal based on your webserver
All it was because Apachi doesn't know what to do with all browser requests. For this reason I've created a simple .htaccess file:
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]
After this I needed to use rewrite_module in my WAMP settings.
It perfectly works with project when I open it in browser through the explorer.
And after this I set up my server like this:
And that allows me to preview all my changes through PHPStorm

Symfony2 remove web from the url on the external server

I've been looking for the solution for a long time. There are a lot of such topics and I know it. But I still can't figure it out. How can I remove web from the symfony2 project url? I tried to do this with htaccess looking like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]
but it doesn't work. It gives me an error:
No route found for "GET /web/"
I really can't change the root directory on this server as only thing I have is FTP permission. Anybody has any idea for this?
Edit: I also tried a trick to put all the files from web in root directory and the rest of the project higher. The problem is the highest directory I have access to is the root.
You shall need to update your virtual host config file and set the DocumentRoot to point to the web folder.
For example you may currently have it set as
DocumentRoot /var/www/myproject/
but you need to update it to
DocumentRoot /var/www/myproject/web
More information can be found within the Symfony2 cookbook here
Also this shall stop anyone trying to access the config/parameters.yml

Slim Framework always return 404 Error

These days i'm using Slim Framework as my simplest tool to develop the php web api.
Using these two articles:
Coenraets
CodingThis
I follow some of the steps from there. Downloading the Slim Framework, putting the correct directory & files. Adjusting the initation statements such as;
//1. Require Slim
require('Slim/Slim.php');
//2. Instantiate Slim
$app = new Slim();
//3. Define routes
$app->get('/books', function ($id) {
//Show book with id = $id
});
And then, I modify the rest accordingly.
Such as my checklist that already done:
LoadModule rewrite_module modules/mod_rewrite.so -> enabled
Slim .htaccess:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule
^(.*)$ bootstrap.php [QSA,L]
httpd.conf: (Shared Link).
But, after Once I run this statement;
$app->run();
And I run it on my browser.... then, I got 404 Error while testing it on my Localhost. What's the solution for fixing that?
FYI, here is my simplest PHP file that i'm currently using it. (shared Link)
Problem is solved!
My apache is actually normal, and the .htaccess file provided earlier also normal.
The clue is the URL that I used.
Previously I used the invalid URL, thus it returned the 404 page error.
I just realized it when I Tried to access the newer GET URL via browser with this one;
http://localhost/dev/index.php/getUsers/user1
and now that works!
I just realized it once I found these statements;
If Slim does not find routes with URIs that match the HTTP request
URI, Slim will automatically return a 404 Not Found response.
If Slim finds routes with URIs that match the HTTP request URI but not
the HTTP request method, Slim will automatically return a 405 Method
Not Allowed response with an Allow: header whose value lists HTTP
methods that are acceptable for the requested resource.
I found this post while googling "slimframework 404". The post led me to a solution to my problem.
The Problem
I set up a site with the Slim framework using Composer and create an index.php with the following example code form slimframework.com:
<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
Then I try to access the page using http://localhost/hello/bob. I get back a 404 Page Not Found page.
I was able to get to access the page using http://localhost/index.php/hello/bob.
The Solution
I found the solution by looking at /vendor/slim/.htaccess (included w/ Composer Slim install) and the URL Rewriting section of the Slim framework documentation.
I added a copy of the /vendor/slim/.htaccess file to the same folder as my index.php file. The contents of the file are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Now I can access the page using http://localhost/hello/bob.
You're right about to include the index.php on your file, but that happen because you're not working properly with your mod-rewrite
Please check the following link:
https://github.com/codeguy/Slim
In the Get Started part you could see how to configure your server (in case that you were using apache / lighttpd / ngynx)
I think your problem is at the "Resource parser" because you are no defining $id parameter at the request so, please, try this:
//1. Require Slim
require('Slim/Slim.php');
//2. Instantiate Slim
$app = new Slim();
//3. Define routes
$app->get('/books/:id/', function ($id) {
echo json_encode( getBook($id) );
});
// some stuff
$app->run();
Please, tell us if it's ok
For me the problem was that I'd forgotten to provide a .htaccess file in my document root.
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you're using Slim on Ubuntu 16.04 and you're getting the 404 error. Try this test from Tod Birdsall above:
If this works
http://localhost/index.php/hello/bob
and this doesnt work
http://localhost/hello/bob
and your .htaccess file in the same directory as your index.php and is configured as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
And you still get the 404 error on Apache.
Try turning on Apache mod_rewrite as follows and restart Apache:
$sudo a2enmod rewrite
$sudo service apache2 restart
The Slim API should work correctly now as the provided .htaccess file will only work if mod_rewrite is enabled and that not the default on a clean install.
Here is how to turn off mod_rewirte if you need to.
$sudo a2dismod rewrite
$sudo service apache2 restart
Additionally mod_rewrite.so module must be loaded in httpd.conf or you will receive error 500.
LoadModule rewrite_module modules/mod_rewrite.so
For people who still search answers because previous doesn't work, this works for me :
RewriteBase /<base_of_your_project>/

CodeIgniter 404 Page Not Found, but why?

I am using CodeIgniter for two applications (a public and an admin app).
The important elements of the document structure are:
/admin
/admin/.htaccess
/admin/index.html
/application
/application/admin
/application/public
/system
.htaccess
index.php
The /admin/.htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
The /admin/index.php has the following changes:
$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)
And the /application/admin/config/routes.php contains the following:
$route['default_controller'] = "welcome";
$route['admin'] = 'welcome';
Welcome is my default controller.
When I call up the Domain/admin I get a 404 Page Not Found error. When I call up the Domain/admin/welcome everything works fine. In the debug logs I get the following error message:
DEBUG - 2010-09-20 16:27:34 --> Config Class Initialized
DEBUG - 2010-09-20 16:27:34 --> Hooks Class Initialized
DEBUG - 2010-09-20 16:27:34 --> URI Class Initialized
ERROR - 2010-09-20 16:27:34 --> 404 Page Not Found --> admin
Weirdly enough this setup works perfectly on my local MAMP installation (with the localdomain/admin/), but when I publish and test it on the "live" server, I just get 404 errors.
Any ideas? What am I doing wrong?
Thanks
C.
The cause of the problem was that the server was running PHP using FastCGI.
After changing the config.php to
$config['uri_protocol'] = "REQUEST_URI";
everything worked.
You could try one of two things or a combination of both.
Be sure that your controller's name starts with a capital letter. eg "Mycontroller.php"
If you have not made any changes to your route, for some strange reason, you might have to include capital letters in your url. e.g if your controller is 'Mycontroller.php' with a function named 'testfunc' inside it, then your url will look like this: "http://www.yourdomain/index.php/Mycontroller/testfunc". Note the capital letter. (I'm assuming you haven't added the htaccess file to remove the 'index.php' part. If you have, just remove it from the url.)
I hope this helps someone
Leaving this answer here for others who ran into my situation.
My codeigniter app was working fine in localhost/WAMP, but was unable to route and produced 404 not found errors when pushing to an AWS EC2 instance. My issue was solved from the answer from HERE htaccess works in localhost but doesn't work in EC2 instance
(route to my admin page)
{domain}/admin was producing 404
the /etc/httpd/conf/httpd.conf file needs to be modified.
-after every instance of "DocumentRoot "/var/www/html"" (2 places) "AllowOverride None" needed to be changed to "AllowOverride All".
Restarted the EC2 instance from the AWS dashboard.
{domain}/admin is now accessible and working as intended.
hope this helps someone else like it helped me!
Change your controller name first letter to uppercase.
Change your url same as your controller name.
e.g:
Your controller name is YourController
Your url must be:
http://example.com/index.php/YourController/method
Not be:
http://example.com/index.php/yourcontroller/method
we have to give the controller name in lower cases in server side
$this->class = strtolower(__CLASS__);
If you installed new Codeigniter, please check if you added .htaccess file on root directory.
If you didn't add it yet, please add it.
You can put default content it the .htaccess file like below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Modify apache config file as mentioned below :
sudo nano /etc/apache2/apache2.conf
Goto line no 172 and change
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
and then finally
sudo systemctl restart apache2
Your folder/file structure seems a little odd to me. I can't quite figure out how you've got this laid out.
Hello I am using CodeIgniter for two applications (a public and an admin app).
This sounds to me like you've got two separate CI installations. If this is the case, I'd recommend against it. Why not just handle all admin stuff in an admin controller? If you do want two separate CI installations, make sure they are definitely distinct entities and that the two aren't conflicting with one another. This line:
$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)
And the place you said this exists (/admin/index.php...or did you mean /admin/application/config?) has me scratching my head. You have admin/application/admin and a system folder at the top level?
In my case I was using it on localhost and forgot to change RewriteBase in .htaccess.
I had the same issue after migrating to a new environment and it was simply that the server didn't run mod_rewrite
a quick sudo a2enmod rewrite
then sudo systemctl restart apache2
and problem solved...
Thanks #fanis who pointed that out in his comment on the question.
If your application is in sub-folder then the Folder name in directory and URL must be same (case-sensitive).
It happens cause of multiple reasons but the answer missing above there's the "className" while extending your controller.
Make sure your class name is the same as your controller name is your controllers. e.g.,
If your controller name is Settings.php, you must extend the controller like.
class Settings extends CI_Controller
{
// some actions like...
public function __construct(){
// and so and so...
}
}

Categories