[英]CakePHP REST webservice and authentication, how to handle
我已遵循有关CAKEPHP中身份验证的指南http://www.google.com/url?sa=D&q=http://book.cakephp.org/view/1250/Authentication
我遵循了有关如何设置REST Web服务的指南: http : //book.cakephp.org/view/1239/The-Simple-Setup
这正是我的app_controller:
class AppController extends Controller {
var $components = array('DebugKit.Toolbar', 'Session', 'Auth');
var $helpers = array('Html','Javascript', 'Session', 'Ajax', 'Facebook.Facebook');
function beforeFilter()
{
// importa modello Season
$this->Season = ClassRegistry::init('Season');
// ricava la variabile di sessione
$id = $this->Session->read('editable_season_id');
// verifica se tale id è esistente
$exist = $this->Season->exist($id);
// se non esiste o è un valore non valido o è nullo
if((is_null($id)) || (!is_numeric($id) || (!$exist)))
{
// cerca l'id della stagione più recente
$id = $this->Season->getLastSeasonId();
// se esiste assegnalo ad una var di sessione
if($id)
{
$this->Session->write("editable_season_id", $id);
$title = $this->Season->getSeasonName($id);
$this->Session->write("editable_season_title", $title);
$this->redirect($this->referer());
} else { // altrimenti lancia errore
$seasons = $this->Season->getSeasons();
$this->cakeError('defaultSesonNotFound', array('seasons' => $seasons));
}
}
}
}
现在,在每个控制器中,我都覆盖了beforeFilter函数,并允许执行某些操作。 例如,我想与所有人共享我的REST,因此在我的控制器中:
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('showMatchesBySeasonId', 'matchDetails', 'view');
}
其中view是我的REST服务的名称。 第一次调用VIEW函数时,我将重定向到登录操作。 如果我再次调用VIEW,它将被正确执行并显示信息,直到名为“ CAKEPHP”的cookie存储在我的计算机中(如果我刷新内存缓存,也将出现同样的问题),这将是没有问题的。 为什么? 如何避免这种行为?
问候!
我注意到Session组件发生了类似的问题。 尝试改用Cookie。
class AppController extends Controller {
var $components = array('DebugKit.Toolbar', 'Cookie', 'Auth');
var $helpers = array('Html','Javascript', 'Ajax', 'Facebook.Facebook');
function beforeFilter()
{
// importa modello Season
$this->Season = ClassRegistry::init('Season');
// ricava la variabile di sessione
$id = $this->Cookie->read('editable_season_id');
// verifica se tale id è esistente
$exist = $this->Season->exist($id);
// se non esiste o è un valore non valido o è nullo
if((is_null($id)) || (!is_numeric($id) || (!$exist)))
{
// cerca l'id della stagione più recente
$id = $this->Season->getLastSeasonId();
// se esiste assegnalo ad una var di sessione
if($id)
{
$this->Cookie->write("Season.editable_id", $id);
$title = $this->Season->getSeasonName($id);
$this->Cookie->write("Season.editable_title", $title);
$this->redirect($this->referer());
} else { // altrimenti lancia errore
$seasons = $this->Season->getSeasons();
$this->cakeError('defaultSesonNotFound', array('seasons' => $seasons));
}
}
}
}
另外,您会注意到我将Cookie-> write()项设置为“ Season.editable_id”。 您可以使用“。”将sesssion / cookie用作多维数组。 我发现它是更好的做法,这样您就不会覆盖键/值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.