Jump to content

Hook for $page->httpUrl()?


chrizz
 Share

Recommended Posts

I already checked the CaptainHook cheatsheet - only the httpUrl method for PageFiles is hookable. It looks as if hooking the counterpart for pages is not possible ?

I currently use httpUrl at many places in the code but the URLs need to be rewritten. When hooks are not appropriate: Is there a way to add a custom method for all pages (e.g. $page->myCustomPath()) which actually returns the URL I need?

Link to comment
Share on other sites

When you want to change a page's url or httpUrl in a hook you do this by hooking Page::path - the URLs are derived from the path.

The classic post about this is Ryan's CMSCritic case study: 

So for a hook like this...

$wire->addHookBefore('Page::path', function(HookEvent $event) {
	$page = $event->object;
	if($page->template == 'news_item') {
		$event->replace = true;
		$event->return = "/my-custom-path/$page->name/";
	}
});

...the results are...

2019-06-11_110639.png.dcf2681c17c1c5118fa417205965ee29.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...