simple php Slim project errors - php

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

Related

Deployed new Laravel project to live server - inner page links do not work

I have recently deployed a Laravel project to my live web server via FTP (Filezilla). Inside my young1.org web root folder I have the subdomain folder bookings, which displays web content at http://bookings.young1.org. Inside that folder I have the folder, 'laravel' that contains my entire laravel application, and inside that folder there is a 'public' directory.
I have imported my local database to one of the database accounts on the live web server via phpmyadmin, and I have switched the 'DB' credentials to point to the new database inside the env file in the laravel project root (changing the following variables: DB_DATABASE, DB_USERNAME, DB_PASSWORD).
When I navigate to http://bookings.young1.org/laravel/public, the home page of my application appears, fine and dandy. However, when I click on any of the internal links (e.g. the login and register) buttons, I just get a series of blank pages, and none of the internal pages appear.
Would anyone be able to take a guess at what the problem might be?
I have tried altering the .htaccess file to look like the below, and changing my 'PATHS' variable inside public/index.php.
Thanks,
Robert
London, UK
// public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
// public/index.php
require DIR.'/../laravel/bootstrap/autoload.php';
$app = require_once DIR.'/../laravel/bootstrap/app.php';
do you have SSH on the server? if yes did u install laravel as you're supposed to, also could you include the .env file contents, you can mask out the DB username and pass and the key you had to generate
never mind my mistake, i clicked on a link and got a SQL connection error (refused)
i still need to know if you installed laravel via SSH or that you just made the public folder the root, because if that's the case laravel cant help you with that (you need a VPS and not a webhost that only supports FTP as far as i know)
and to be sure
BE CAREFULL!!!
you have SQL connection errors that show credentials
Notice that your URLs work fine if you use index.php in them, i.e.:
http://bookings.young1.org/laravel/public/index.php/register
To allow URLs without index.php the mod_rewrite module on your Apache web server must be enabled.
First try to add this line in your .htaccess file of Laravel above RewriteEngine On:
Options +FollowSymLinks
This directive is needed to enable mod_rewrite in .htaccess context.
If it doesn't work after this, then you can check if module is enabled on your web server, the easiest maybe is to paste this in the beginning of index.php in public folder:
phpinfo();
Then open any page and search for mod_rewrite on the page, and see if you can find it under Loaded Modules. If not, you have to enable it.
To do that, if you can access through SSH you can do:
sudo a2enmod rewrite
sudo service apache2 restart
For more help on enabling mod_rewrite on Apache web server, check this answer.

How to direct load to web folder

I have just installed Yii2. I am still new in this framework. When I created new project using composer named "Sample-Yii2", I must open "web" folder to load index.php. I am just curious if this index.php can be moved to root folder. But it cant.
How I can get direct page when I open localhost/Sample-Yii2 instead loacalhost/Sample-Yii2/web?
Use the following configuration in Apache's httpd.conf file or within a virtual host configuration. Note that you should replace path/to/basic/web with the actual path for basic/web.
# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"
<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
Reference here. Also read the article.
If you use apache as a web server in development, you should simply properly configure a virtual host in apache config files as described in other . May be it does not worth it. For example if you are just playing around with the new framework (Yii2). Try to use PHP's Built-in Server instead. It's much simpler to use.
cd Sample-Yii2/web
php -S localhost:8000
Go to your browser and visit localhost:8000
For more details see here

Error 404 with slim framework

I am learning to code in PHP and mysql and need to be able to run PHP files using a wampserver. I have correctly installed wamp and have it online through Windows 8, however when I go to run my PHP file I am always given Error 404. I have ensured that my Slim file and .php file are in the correct directory and I have also modified the .htaccess accordingly so that it reroutes to the proper file if a URL is not found since I am testing this on my local machine. Here is a some of my sample code:
require 'C:\wamp\www\easysites\codeguy-Slim-b8181de\Slim\Slim.php';
\Slim\Slim::registerAutoloader();
$api = new \Slim\Slim();
$api->run();
I have traced the error to $api->run() and was getting an error from the call stack at one point that indicated an error had happened in Slim's environment.php at line 123 where the URL is first dealt with. If I run the basic index.php file generated by Netbeans, I get the usual text that alerts me that it is working, however try to run the php file using Slim it errors out which are in the same sub directory under root. Slim is correctly loaded by the php file, but will not allow me to run anything else thus preventing me from seeing the database that I have imported.
Does anyone have an idea of what could be going wrong? I have a co-worker who has successfully run the file but we are unable to make it run on my machine using the same types of changes.
The .htaccess file is as follows:
RewriteEngine ON
RewriteRule ^API(.*)$ /API/api.php [QSA, L]
The file I am trying to run is in a folder contained in the root directory named API, and the file I am trying to run is api.php.
When I followed all the instructions of the installation of slim exactly (installing it on easyphp on my local windows machine), I got the error 404.
I changed the .htaccess to the following, and then it worked fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Appfog PHP Slim Application throws error when trying to access the API

I'm trying to setup a REST application in Appfog using Slim framework. This was based on a tutorial by Coenrats in this link:
http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/
only I used a newer version of the framework. Folder structure are as follows
app
--> api
----> .htaccess
----> index.php
----> Slim framework folder
--> js
----> jquery.js
----> main.js
--> css
----> styles.css
--> index.html
Here's the code on how I Slim
// load required files
require 'Slim/Slim.php';
// register Slim auto-loader
\Slim\Slim::registerAutoloader();
// initialize app
$app = new \Slim\Slim();
Here's my .htaccess code:
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} !-f
RewriteRule ^ index.php [QSA,L]
Since I'm doing REST, I have setup a rootURL also as per the tutorial which is:
var rootURL = "http://test.ap01.aws.af.cm/app/api/";
When I run the application, I'm calling a get method to fetch all the data in the database but this error shows up in my console:
GET http://test.ap01.aws.af.cm/app/api/ 404 (Not Found)
Could I be missing something?
Thanks in advance.
Cheers!
Jason
I think I found out the cause of the error, I included /app in the url which should not be. My mistake on this one. Thought I should include the whole URL when I checked the app directory.
Thanks!

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>/

Categories