Jump to content

Adding Custom Methods to All Pages


leode
 Share

Recommended Posts

Hello Guys!

I read the article https://processwire.com/blog/posts/pw-3.0.152/ and want to add some custom methods to all of my pages. As I understand, when I add /site/classes/DefaultPage.php with class DefaultPage extends Page {} and put in my properties and methods, they should be available to all pages.

Here is the code I tried:

<?php namespace ProcessWire;

class DefaultPage extends Page {
    public function test() {
       return 'test';
    }
}

However, this does not work (Exception: Method ArticlePage::test does not exist or is not callable in this context)

page()->get('/mytestpage/')->test()

I would have guessed that since HomePage extends Page and all Page instances are now DefaultPage, my test() method would work.

Is there no other way than using hooks to add custom methods to have them on all pages (HomePage, basic-page, my custom ones, etc.)?

Thank you for your help!

 

Link to comment
Share on other sites

@leode, you'll have to make sure your page classes now extend DefaultPage, rather than Page:

<?php namespace ProcessWire;

class ArticlePage extends DefaultPage //not Page
{
  //your article page specific methods
}
?>

<?
pages()->get('/mytestpage/')->test(); //should now work

 

  • Like 2
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...