phproutesflightphp

I don't understand routing


I am trying to learn a PHP framework. But I'm having some difficulties understanding some of the concepts of routing.

I've chosen to use Flight. Their homepage shows:

require 'flight/Flight.php';

Flight::route('/', function(){
    echo 'hello world!';
});

Flight::start();

And I don't understand what they're using Flight::route... for. What am I missing? This question isn't even related to Flight. It's related to just routing in general.


Solution

  • What seems to be happening in your file (I'm not familiar with Flight)

    the require 'flight/Flight.php'; is more than likely defining a class for all the routing.

    Then Flight::route(); Is simply using the route() method from the class Flight without an instance of the class.

    Flight::route('/', function(){
        echo 'hello world!';
    });
    

    What happens here is when a route is matched (by matched means the URI of the user matches the URI on your route, in this case www.yourdomain.com/ will match the '/' route) And then the code inside the function() callback gets executed.

    If you add another route

    Flight::route('/about', function(){
        echo 'About Us';
    });
    

    When the user visits www.yourdomain.com/about He will get whats inside that route.