0

I have a route that takes data from APIs, so I would like to set the title in the controller and not in the getTitle function because it would mean calling 2 or more times the same API

  • 1
    You can return a title by setting #title in the top-level render array that you return – Berdir Jun 2 '17 at 11:19
0

I don't think you can, not out of the box at least, but there's no reason to. Just use a static var, it will mean the API is contacted only once per request:

function getTitle() {
  static $title = '';

  if (!strlen($title)) {
    $title = $this->getTitleFromAPI();
  }

  return $title;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.