Well, I found a way to implement another controller hook that is
called between initialization of components and execution of action
logic:

<?php
/**
 * Enables another hook (_afterComponentsInitialization()) that is placed
 * between the initialization of components and the execution of action logic.
 *
 * So the new sequence is:
 * - beforeFilter()
 * - initialization of components
 * - _afterComponentsInitialization()
 * - action logic
 * - beforeRender()
 * - rendering logic
 * - afterFilter()
 *
 * To work properly, AfterComponentsInitializationFilter needs to be the last
 * Item in the $components array:
 *
 * class AppController extends Controller {
 *   var $components = array(
 *     'SomeComponent',
 *     'AnotherComponent',
 *     'AfterComponentsInitializationFilter'
 *   );
 *
 *   function _afterComponentsInitialization() {
 *     // Do something between initialization of components and action logic!
 *   }
 * }
 */
class AfterComponentsInitializationFilterComponent extends Object {
  function startup(&$Controller) {
    $Controller->_afterComponentsInitialization();
  }
}

You think this is a good way? :-)

On Fri, Nov 12, 2010 at 10:59 AM, Joshua Muheim <psybea...@gmail.com> wrote:
> Oh, and by the way: why is the "filter" in beforeFilter resp.
> afterFilter? beforeRender makes sense to me, but beforeFilter seems a
> strange name to me...
>
> On Fri, Nov 12, 2010 at 10:30 AM, Joshua Muheim <psybea...@gmail.com> wrote:
>> I'm using a component called SimpleAuth and SimpleAcl (instead of the
>> standard Auth and Acl ones because they are too complicated for my
>> small project).
>>
>> SimpleAuth features a getActiveUser() method which returns the
>> currently logged in user. This method calls $this->Controller, which
>> is only available when the component's startup(...) method was called
>> from CakePHP.
>>
>> In my view I'd like to have a $user object contains all the user's
>> data and which can also handle stuff like $user->isAllowedTo(...) or
>> $user->isGuest(). I have created some UserProxy class for this:
>>
>> class UserProxy {
>>  var $auth;
>>  var $acl;
>>  var $guestId = 1;
>>
>>  function __construct($auth, $acl) {
>>    $this->auth = $auth;
>>    $this->acl = $acl;
>>    $this->user = $this->auth->getActiveUser(); // This line will be 
>> important!
>>  }
>>
>>  function name() {
>>    return $this->user['User']['name'];
>>  }
>>
>>  function isGuest() {
>>    return $this->user['User']['id'] == $this->guestId;
>>  }
>> }
>>
>> To automatically have this $user object available in my views, I want
>> to set it in a beforeFilter().
>>
>>  function _prepareUser() {
>>    $this->user = new UserProxy($this->SimpleAuth, $this->SimpleAcl);
>>    $this->set('user', $this->user);
>>  }
>>
>> This requires the SimpleAuth and SimpleAcl components to already be
>> initalized, because in $this->auth->getActiveUser() in the constructor
>> of UserProxy the SimpleAuth components needs access to the controller
>> and the session:
>>
>>  function getActiveUser() {
>>    if 
>> ($activeUserId=$this->Controller->Session->read($this->userModel.'.id'))
>> {
>>      ...
>>    }
>>    ...
>>  }
>>
>> And because the startup() method doesn't seem to be called at this
>> time already, I get the following error:
>>
>> Warning: Trying to get property of non-object
>> [APP/controllers/components/simple_auth.php, line 120]
>> SimpleAuthComponent::getActiveUser() -
>> APP/controllers/components/simple_auth.php, line 120
>> UserProxy::__construct() - APP/controllers/app_controller.php, line 10
>> AppController::_prepareUser() - APP/controllers/app_controller.php, line 37
>> AppController::beforeFilter() - APP/controllers/app_controller.php, line 28
>> Controller::startupProcess() -
>> CORE/cake/libs/controller/controller.php, line 526
>> Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 187
>> Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
>> [main] - APP/webroot/index.php, line 83
>>
>> Fatal error: Call to a member function read() on a non-object in
>> /Users/josh/Sites/working_tests/checking_out_simple_auth_and_simple_acl/app/controllers/components/simple_auth.php
>> on line 120
>>
>> So I need a filter that's called before the components are initialized
>> (that's the beforeFilter()) and a filter that's called after the
>> initialization of the components but before the controller and view
>> codes are processed.
>>
>> So @Miles: no, the beforeFilter() can't be executed after the
>> components' initialization, because often we set component-specific
>> parameters in the beforeFilter, like
>>
>> function beforeFilter() {
>>  $this->Security->blackHoleCallback = '_blackHole';
>>  parent::beforeFilter();
>> }
>>
>> Anyone got an idea on how to accomplish this? IMHO there should be a
>> beforeComponents() and an afterComponents() filter...
>>
>> On Thu, Nov 11, 2010 at 8:43 PM, Miles J <mileswjohn...@gmail.com> wrote:
>>> Also forgot to mention that there is an afterFilter() as well.
>>>
>>> On Nov 11, 10:30 am, Miles J <mileswjohn...@gmail.com> wrote:
>>>> Components are initialized before they get to beforeFilter(), so
>>>> beforeFilter() should work fine.
>>>>
>>>> https://github.com/cakephp/cakephp/blob/master/cake/libs/controller/c...
>>>>
>>>> On Nov 11, 9:24 am, Bogdan Bursuc <bogdanbursu...@gmail.com> wrote:
>>>>
>>>> > Why do you need one ?
>>>>
>>>> > On Thu, Nov 11, 2010 at 7:18 PM, psybear83 <psybea...@gmail.com> wrote:
>>>> > > Hi all
>>>>
>>>> > > Is there a filter that runs after the initialization of components but
>>>> > > before beforeRender?
>>>>
>>>> > > Thanks for help
>>>> > > Josh
>>>>
>>>> > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others
>>>> > > with their CakePHP related questions.
>>>>
>>>> > > You received this message because you are subscribed to the Google 
>>>> > > Groups
>>>> > > "CakePHP" group.
>>>> > > To post to this group, send email to cake-php@googlegroups.com
>>>> > > To unsubscribe from this group, send email to
>>>> > > cake-php+unsubscr...@googlegroups.com<cake-php%2bunsubscr...@googlegroups.com>For
>>>> > >  more options, visit this group at
>>>> > >http://groups.google.com/group/cake-php?hl=en
>>>>
>>>> > --
>>>> > Thanks,
>>>> > Bogdan Iulian Bursuc
>>>
>>> Check out the new CakePHP Questions site http://cakeqs.org and help others 
>>> with their CakePHP related questions.
>>>
>>> You received this message because you are subscribed to the Google Groups 
>>> "CakePHP" group.
>>> To post to this group, send email to cake-php@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
>>> http://groups.google.com/group/cake-php?hl=en
>>>
>>
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to