1

I started today with the PHP Framework Flight. Now I downloaded the "Flight skeleton" from their homepage.

Here is an image of the folder structur:

enter image description here

Here is the that is inside the index.php file:

<?php

    require 'flight/Flight.php';
    
    Flight::route('/', function(){
        echo "Hello World";
    });

    Flight::route('/categorie', function(){
        echo "Hello World";
    });

    Flight::start();

?>

If I start the browser and enter the URL http://localhost/firstflight/ I get "Hello World" displayed. But if I enter http://localhost/firstflight/categorie I get an error 404 Webpage not found... displayed. How can I fix this problem?

Here is the code inside the .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
MyNewName
  • 1,035
  • 2
  • 18
  • 34
  • You need to do some adjustments when running in a sub directory (you really should just create a virtual host and run it that way) http://stackoverflow.com/questions/20890210/flight-php-routing-from-subdirectory – JimL Mar 15 '16 at 19:54
  • What do you mean with "you really should just..." ? – MyNewName Mar 15 '16 at 19:56
  • You can create a virtual host (in apache, nginx, etc) that will map some domain (ie flightapp.local) to a spesific folder (yourwww/firstflight). then you can just add `127.0.0.1 flightapp.local` to your computers hosts file and you can access `http://flightapp.local` – JimL Mar 15 '16 at 19:57
  • @JimL now I am a little bit confused. I have xamp running at the moment, and I can access the webpage. The problem is, that I get an error (`404 Error Page`) displayed when I try to open the route `http://localhost/firstflight/categorie`. The other route `http://localhost/firstflight/` is working well. – MyNewName Mar 15 '16 at 20:01
  • Yeah and you either have to do some changes to make it work in a sub directory, or you have to not have the sub directory (ie by using a virtual host) – JimL Mar 15 '16 at 20:02

4 Answers4

0

I had the same problem, but when I configure apache2.conf file (/etc/apache2) it works. In there you have to change AllowOverride.

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

then my .htaccess file look like this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

That's all.

0

This worked for me. In your .htaccess file, paste the lines of code below.

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

It the same ones used in Laravel - a more advanced PHP Framework.

Make sure the .htaccess is in the same folder/ directory/ level as your index.php file.

MosesSoftEng
  • 485
  • 5
  • 13
0

In my case, it was default apache2.conf that caused Flight to fail.

Had to change

<Directory /var/www/>
    AllowOverride None
</Directory>

to

<Directory /var/www/>
    AllowOverride all
</Directory>

This way my .htaccess actually is applied. And then everything works as it should.

Dima Stefantsov
  • 943
  • 1
  • 14
  • 20
0

Routes are matched in the order they are defined. The first route to match a request will be the one that is invoked.

You can use regular expressions in your routes:

Flight::route('/user/[0-9]+', function(){

    // This will match /user/1234
});
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
Mamunur Rashid
  • 1,095
  • 17
  • 28