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.