Let's say I have a component
- looky
- Member | 99
..called from template via {control myComponent} and I want it to be able to check if the user is logged in on it's creation, and if he isn't, redirect to login page. So in presenter's createComponentMyComponent() I pass $this->getUser() to my constructor, save it to property, and check in attached() (because there I have a presenter to call redirect on).
This works fine, except for one small detail – it ends with 500 server error (on production, on dev it just throws AborException).
So, how do I achieve the requested functionality?
- Majkl578
- Moderator | 1364
Your component is created too late, trying to redirect while the template is
already rendering. You'd have to enforce creating it sooner, e.g. in
startup/action method, e.g. by calling
$this->getComponent('myComponent')
or just
$this['myComponent']
.
Also, technically you don't even need to pass User to the component – you can
obtain it through Presenter#getUser().