Forgive me if I have misunderstood, but it sounds as if you are trying to rebuild something that comes for free with Cake. Have you read about the Auth component? This manages authentication, your logged in state and prevents you from running functions that you must be logged in for. If you are not logged in, it automatically redirects you to the log in page. When you successfully log in, it (usually) redirects you to the page you were trying to reach. The details of the currently logged in user are held in a freely available session variable too.
Can I suggest you read this part of the guide and then give this tutorial a good read through. The tutorial takes you into ACL management too, which might be a step too far right now - but you'll get the authentication basics. Jeremy Burns [email protected] On 8 Apr 2010, at 01:53, Luck wrote: > Hi experts, > > I'm new with CakePHP and I'm trying to build a simple component to > check if I'm logged (reading a value in session). > > I created the component so I'm calling the function in controllers > beforeFilter, to check the user state from every action. The idea is > that if I'm not logged, it redirects me to login page. > The problem it's failing with the subject's message. Strange thing is > that if I call the component function from each action, it works. It's > like if the parent controller was unknown in the component for some > reason. > > Can you please help me? > Thanks in advance! > > Now the code: > > The component is StateControl > The login validation function is checkState > The controller where I want to validate login is ProductsController > > COMPONENT: state_control.php > ========== > <?php > class StateControlComponent extends Object { > var $controller; > var $components = array('Session','RequestHandler'); > > function startup(&$controller) > { > $this->controller =& $controller; > } > > function checkState() { > if (!$this->Session->read('username')) { > $this->Session->setFlash('You are not logged. > Please enter your > credentials.'); > $this->controller- >> redirect(array('controller'=>'users','action'=>'login')); > } > } > } > ?> > > > CONTROLLER USING FUNCTION IN BEFORE FILTER > ========================================= > <?php > class ProductsController extends AppController { > var $name = 'Products'; > var $helpers = array('Html', 'Form'); > var $components = array('Acl','StateControl'); > > function beforeFilter() > { > $this->StateControl->checkState(); > } > > .... > // controller actions > .... > } > > > > EXECUTION > ========= > When I call http://localhost/cake/pr1/products/edit/22 > > i'm getting: > "Fatal error: Call to a member function redirect() on a non-object in > C:\wamp\www\cake\pr1\controllers\components\state_control.php on line > 14" > > NOTES > ===== > - Line 14 is: $this->controller- >> redirect(array('controller'=>'users','action'=>'login')); > - I'm "logged" when I have 'username' value in Session > The component works fine if I call $this->StateControl->checkState(); > from every controller action and not beforeFilter > The component works fine if I call $this->StateControl->checkState(); > in controller beforeFilter and I'm logged > The component fails if I call $this->StateControl->checkState(); in > controller beforeFilter when I'm not logged > > 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 [email protected] > To unsubscribe from this group, send email to > [email protected] For more options, visit this group at > http://groups.google.com/group/cake-php?hl=en > > To unsubscribe, reply using "remove me" as the subject. 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
