Yii Framework Forum: How to get controller inside component - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to get controller inside component Rate Topic: ***** 1 Votes

#1 User is offline   juhe 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 18-October 09

Posted 16 November 2009 - 07:03 PM

Tried different time points to get the controller, but it`s always null.
This is how I try to access it:
Yii::app()->controller

I tried to recieve it onEndRequest event and also in init(). Both resolves to null.

I need to do some session-based state handling inside component which depends on the controller at hand.
0

#2 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 17 November 2009 - 04:57 PM

The component must be loading before the controller does. Is this component set to autoload? Why do you need the controller called from the component? This is not common
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#3 User is offline   juhe 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 18-October 09

Posted 17 November 2009 - 06:35 PM

View Postjonah, on 17 November 2009 - 04:57 PM, said:

The component must be loading before the controller does. Is this component set to autoload? Why do you need the controller called from the component? This is not common

onEndRequest is certainly not before controller:)

Actually why is it that so uncommon? Even if we take authorization, this also depends on controller name.

In my case I delete search query when not in specific set of pages.
0

#4 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 17 November 2009 - 07:20 PM

onEndRequest() is a method of CApplication, not CComponent.. Putting onEndRequest() in a component won't do anything. Unless you mean you are calling the component in onEndRequest(). How is the component called?
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#5 User is offline   megahertz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 04-March 09
  • Location:Russia, Yekaterinburg

Posted 18 November 2009 - 12:19 AM

Did you try to use Yii::app()->getController() ?
0

#6 User is offline   juhe 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 18-October 09

Posted 18 November 2009 - 11:45 AM

yes I tried using getController and i registered component method as onendrequest handler. If you look in the code it resets app controller back to $oldcontroller, but $oldcontroller is NULL. why is this swap necessary?
0

#7 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 19 November 2009 - 08:05 AM

View Postjuhe, on 18 November 2009 - 11:45 AM, said:

yes I tried using getController and i registered component method as onendrequest handler. If you look in the code it resets app controller back to $oldcontroller, but $oldcontroller is NULL. why is this swap necessary?

The swap I believe your talking about is required in the cases where a component is used, and the controller needs to be the current controller. OnEndRequest there is no active controller so you should get null.
You probably want to override YiiApp:afterControllerAction()

nz
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users