Jump to content

Template File as Controller - How to access $input, $user, etc.


bracketfire
 Share

Recommended Posts

I'm building an architecture for a PW site where the Template Files are actually Controller classes.  In these controller objects, I'm accessing the API with:

$input = wire('input');
 

But I'd really like to be doing it like:

$this->input
 

My Controller classes don't currently extend any of the PW classes - But I'm guessing they should (and down the road I'll run into more reasons *why* they should) but don't know if I should extend Page, Wire, WireData, etc.  What's the best thing to do, to have access to the API globals that you'd just use as $page in a "flat" non-object/class template file?

Link to comment
Share on other sites

Your simplest best would be to just extend ProcessWire most basic starting point, which is Wire. This enables your class to have access to API variables (through $this), enables any of your methods to be hookable (that you precede with 3 underscores), and makes the file possible to translate with language support. 

Another option is to extend WireData. This is the same as Wire, except that you gain $this->set() and $this->get() methods that are accessible both in and outside of the class. It essentially means your class can function as a shared data container. The set() and get() can also be accessed by $this->key = 'value'; and $value = $this->key. 

The last utility class to mention would be WireArray, which is meant for storing an array of objects. This one is a little more complex than the other two (though it's still quite simple), and I'm guessing it's not what you need in this case, so won't go into detail unless you want me to. 

You don't necessarily have to extend any of PW's classes. But if you want $this->property; access to the API variables, you'd want to implement your own __get() method in your class:

public function __get($property) {
  return wire($property); 
  // or Wire::getFuel($property); 
}
  • Like 5
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...