Here’s what seems like a fairly simple use of the $site object. I have a page and a corresponding controller, and in the controller I’m trying to get the current URL of the page.
Ignore the fact that this is kinda circuitous—this is just a stripped down version to try and get at the problem. Shouldn’t I be able to access the $site object from anywhere, even in a controller? Or are the $site, $pages, and $page variables only available to template and snippet files? The documentation for controllers seems to suggest that I should have access to $site.
You cannot even use $site->uri->path() in a template, it will result in the same error, just try something like $site->url() or $site->page()->uri(), which should work.
Oh interesting. So where would one be able to use $site->uri->path() if not in a template? I tried using that in a plugin and it didn’t work, either. The documentation isn’t too clear on where you can use different top level objects like that.
Also, thanks for helping out with the alternative methods!
$page->url() isn’t returning what I’m seeing in the browser’s address bar; instead I think it’s returning the URL of the page I’m setting in the route: