0

In Laravel 5 ,I can't understand the movement of arguments inside and outside functions ( anonymous functions) like that one

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

How arguments move from .. I really need to know how $id go to Route::get functions.. The syntax is very difficult for me to write without copy n paste.

1 Answer 1

2

The arguments don't magically "move". When you do this, laravel takes the path/function combination and stores them for later. This is a simplified version of what happens:

class Route
{
     private static $GET = array();

     public static function get($path, $callback)
     {
         self::$GET[] = array($path, $callback);
     }
}

Then later, after all the routes are added, it checks what URL the webpage was called with, and finds the path that matches it. There is some internal procedure that takes the $path for each route and converts it to a regular expression like #user/(?P<id>.+)#, so matching is just done with something like preg_match(). Upon a successful hit, it stops and extracts the variables:

'/user/foobar' has the username extracted: array('id' => 'foobar')

It then uses reflection to match the parameters in the callback with the data from the URL.

$callback_reflection = new ReflectionFunction($callback);
$arguments = $callback_reflection->getParameters();
/* some algorithm to match the data and store in $args */
$result = $callback_reflection->invokeArgs($args);

The invokeArgs() method is what would execute your callback with the correct arguments. There isn't really much magic here. See the Router class for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

Very great answer .. Thanx for your effort
No problem. I had to write my own framework before so know all the ins-and-outs of routing and other core behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.