Note on Handlers

When creating routes, becareful about early and lazy evaluations.

For example:

Route\get('/foo', [new FooController(), 'index']);

Is early, which means it will call FooController constructor for each request even if it's not a request to /foo.
To make it lazy you can wrap inside a Closure:

Route\get('/foo', function () {
  $controller = new FooController();
  return $controller->index();
});

Now FooController is called only when there is a match on route /foo.
One downside is that now you have to explictly manage path parameters, on the other hand is a best practice to do so.
It is a good time to validate parameters, convert plain string params to meaninful types on your domain or resolve dependencies.

Route\get('/users/{id}', function (array $params) use ($ioc) {  
  if (!preg_match('/[0-9]+/', $params['id']) {
    $controller = $ioc->resolve(ErrorController::class);
    return $controller->invalid('IDs must be numbers');
  }

  $controller = $ioc->resolve(UsersController::class);
  return $controller->show($params['id']);
});

results matching ""

    No results matching ""