I guess I’m not the only one that uses some sort of “get specific page by UID” on every single project with Kirby.
While I’m sure there are some other options, these are the three methods I use:
- $site->index()->filterBy(‘uid’, ‘UID’)
Unless you use a custom unique ID field for your pages, the UID is the field that should be unique. Using the panel, you can use an UID multiple times though. Just not under the same parent.
Also, it’s obviously possible to change the UID in the panel. And there’s no option to set the UID to be read-only I think.
But what happens when the “get specific page by UID” fails?
When it either doesn’t find a page with the UID or finds multiple pages with the same UID?
For the latter, I noticed it will always just take the first occurrence. I think that’s fine in most cases.
But if there’s no page with the UID, it breaks the frontend. There will be a “Fatal Error” stating “Uncaught Error: Call to a member function url() on boolean…” and no more code will be displayed after that.
One time, a client of mine changed the “News” page name and UID. The whole page was broken after that, and he didn’t even understand what happened. Well, the latest news are displayed on every page…
If “debug” is set to “false” or the error is displayed in an attribute, it’s just a blank page with no visible error message at all.
There were some discussions on the Kirby Forum, and also the wonderful Auto ID plugin by @helllicht I use on some projects. But I still think the UID is the go-to ID for most Kirby pages.
Would it make sense to optimize the error handling for “UID not found”? Or adding an option to lock a page UID in the blueprint?