search

Home  >  Q&A  >  body text

laravel - How to understand Closure $next when defining middleware

<?php
    namespace App\Http\Middleware;
    use Closure;
    class TestMiddleware
    {      

        public function handle($request, Closure $next)
            if($request->input('age')<18)
                return redirect()->route('refuse');
            return $next($request);
        }
    }

In the above code,
1) The closure in use Closure in the third line is a class? how come?
2) What does Closure $next mean in the handle method on line 6?
3) $next($request) on line 9, what is the usage? Is $next a function or a variable?

迷茫迷茫2785 days ago681

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-16 16:56:35

    Closure is a class that comes with PHP to represent anonymous functions! ! Go to the PHP manual and you will understand the questions you asked.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:56:35

    Closure is a class used by PHP to implement closures (anonymous functions).
    http://php.net/manual/zh/class.closure.php
    $next is the bound function.

    reply
    0
  • Cancelreply