1

I'm pretty new to Lumen and I'm following this tutorial to learn a basic authentication with JWT through Lumen: https://medium.com/tech-tajawal/jwt-authentication-for-lumen-5-6-2376fd38d454

Now its this part of code which puts some questionmarks over my head:

$router->group(
  ['middleware' => 'jwt.auth'],
  function() use ($router){
    $router->get('users', function(){
      $users=\App\User::all();
      return response()->json($users);
    });
  }
);

I dont understand what function() use ($router) does? I read the official of PHP on use: https://www.php.net/manual/de/language.namespaces.importing.php And I also looked into an external ressource: https://www.tutorialspoint.com/php7/php7_use_statement.htm

But I guess I would still require some knowledge about the inner workings of Lumen/Laravel to understand whats happening here. Can someone please give me a lift and explain to me in a few lines whats happening here?

Narktor
  • 977
  • 14
  • 34

1 Answers1

4

It is a PHP feature for bringing an outside variable into the scope of the anonymous function / Closure.

$a = 'hello';

$callback = function ($something, $else) use ($a) {
    echo $a;
};

Without that use declaration the $a variable would not be in the scope of that function.

Since something else is executing your callback, you aren't controlling the arguments passed to it, but you can control the variables you are bringing into the scope of it.

"Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct."

PHP Manual - Anonymous Functions Example #3

Additional Information:

If you look at an example of the map function for Laravel's Collection class you will see this:

$multiplied = $collection->map(function ($item, $key) {
    return $item * 2;
});

In this case they are showing you that your callback will have a Collection's item and key passed to it as arguments.

Laravel 6.x Docs - Collections - Available Methods - map

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • Thanks a lot! I read the linked doc as well, but I still don't understand what the arguments are? – Narktor Dec 02 '19 at 11:58
  • don't understand what what arguments are? – lagbox Dec 02 '19 at 12:16
  • Sry, what I mean is: In your example, what use do the arguments $something and $else have, if the only way to pull variables defined outside the scope of the closure into the scope of the closure is to use the keyword "use"? – Narktor Dec 02 '19 at 12:21
  • 1
    the callback ends up getting called as a function, so there "may" be arguments passed to it ... usually what you are using will have documentation with examples that would show if arguments are going to be passed to your anonymous function when called ... if you look at the documentation for Laravel Collections you will see many of its methods take a callback and it will show the arguments that will be passed to the anonymous function – lagbox Dec 02 '19 at 12:23
  • @baryon123 added some additional information – lagbox Dec 02 '19 at 12:39