I don't understand routing

987 views Asked by At

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.

5

There are 5 answers

2
Sandman21dan On BEST ANSWER

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.

0
NicoE On

Flightphp has a rather comprehensive explanation on how to set up routes here.

You should see routes as definitions of how to handle different request patterns. The example on Flight's home page says that if you hit your site root (i.e. /) it will simply return "hello world!" as a response.

If you read further on the Flightphp install page you will notice that all requests are handled by the index.php page. And thus depending on the routes you define it replies with the relevant response defined for that url request pattern.

0
Oliver Adria On

route() is a static function it seems, that means it's not specific to the object, i.e. you can't create an object such as

$flight = new Flight();

and then call

$flight->route(...)

but rather you call it via the class (not an object, which is a specific implementation of the class). You call static functions of a class by using ::, in this case

Flight::route(...)

The content of the route just says, when you encounter '/', do 'X'... and in your case 'X' is

function(){
    echo 'hello world!';
}

in later stages you get to match stuff like

'/' (homepage, i.e. "mywebsite.com/")
'/about-us' (About Us page, i.e. "mywebsite.com/about-us")
'/user/{id}' (User page, i.e. you can pass a parameter such as "mywebsite.com/user/taylor" and then get the user data)

or whatever you want. And instead of just writing the function into the routing file, you can tell the router to go to a specific function (usually a Controller function) and you can do more stuff there.

I hope this helps!

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

This snippet is heart of your project.

This will accept two parameters.

  1. Route

  2. Method to call on this route call

Consider below code snippet, If you are having your project directory http://localhost/flight_project/, when anyone requests this directory, function defined as 'function_here' will be called.

Flight::route('/', 'function_here');

If you have defined route like below,

Flight::route('/user/', function(){
    // do something here
});

when someone access http://localhost/flight_project/user/, the above in-line function gets called.

More info HERE

0
Crembo On

Routing basically maps HTTP requests to your methods/functions.

To put it simply, say you have the route:

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

This is was basically happens:

  1. Client requests example.com/page1
  2. Server sends query to PHP
  3. Your PHP framework parses the request URL
  4. Picks the correct route, in our case, page1/
  5. And finally calls the function you passed in, so basically echo 'page1';