Access values stored inside the config in controllers

I’m looking for a way to store pages names so that I don’t have to keep referencing page('blog') in each of my controllers, as the page name called change from blog to notes as an example.

Can I store these inside my config or as a global variable somehow? I have seen answers covering using a function inside a plugin, but I’m not sure I want to go this route.

Any suggestions?

A config option is useful if the value isn’t changed by the user, e.g.

return [
  'blogpage' => 'blog'
];

In controllers:

$blogPage = page(option('blogpage'));

That doesn’t help though if the page id can be changed in the Panel. In that case, it might be more useful to find the page by template (provided that the template cannot be changed) or by a unique ID.

Unique ID would be a custom field I guess?

Yes, ideally a field that cannot be changed by the user. Many users use the AutoID plugin to auto-assign IDs at page creation.

Quick question - if I go the config route can I access this inside the blueprint?

Doesn’t seem to be possible through a direct query, but you could create a custom site method:

'siteMethods' => [
    'getBlogPage' => function () {
      return $this->find(option('blogpage'));
    }
  ]

Then in blueprint:

parent: site.getBlogPage
1 Like

Perfect