Jump to content

Using int() and render() controller methods together


Ivan Gretsky
 Share

Recommended Posts

Good day everybody! How are you doing @teppo!

Wireframe\Controller has int() and render() methods. Their docblocks say that inti() is to perform early checks and render is the place to pass vars to view. I've been working with this a while but still do not quite grasp how to use them together. I either use one or a another. Demo repo doesn't have any examples with both methods. Could plrease someone in the know share their wisdom (or opinion) on this?

And a more specific question. If I use them together and put checks into init(), how do I avoid triggering render, if those checks do not pass? For example, some fields are empty and regular render should not happen, but an error should be shown instead.

Thanks!

Link to comment
Share on other sites

Hey Ivan,

Sorry for the delay.

Quote

I've been working with this a while but still do not quite grasp how to use them together. I either use one or a another.

In all honesty: usually render() is the one you want. There are very few cases where init() is better, or necessary for that matter.

Behind the scenes init() is triggered as soon as a Controller class instance is created; at the end of the __construct() method. Meanwhile render() is only called when a page is, indeed, rendered. This means that render() gets called one time for each rendered page (e.g. in case you're rendering multiple pages within single request), while init() is called for each item as soon as their controllers have been instantiated.

Personally I've ended up using render() pretty much all the time 🙂

Quote

And a more specific question. If I use them together and put checks into init(), how do I avoid triggering render, if those checks do not pass? For example, some fields are empty and regular render should not happen, but an error should be shown instead.

There is no built-in solution for this, but if you want to stop rendering the page as soon as you detect (in init()) that you don't want to render it, you could e.g. trigger a redirect, or perhaps call wire404(). Now, if you do use this solution, you'll want to make sure that it's not a "page being rendered within another page" type of situation, as that would be pretty problematic.

Alternatively you might be able to call $this->setView() to override the default view file to something specific to an error scenario. I'm not 100% sure of this though, as its not really something I've every done 🙂

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