CakePHP REST Web服务和身份验证,如何处理

时间:2011-05-06 16:28:01

标签: php cakephp cakephp-1.3

我已按照CAKEPHP http://www.google.com/url?sa=D&q=http://book.cakephp.org/view/1250/Authentication

中的身份验证指南进行操作

我按照指南介绍了如何设置REST网络服务: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之前没有问题(如果我刷新内存缓存会出现同样的问题)。为什么?我该如何避免这种行为?

问候!

1 个答案:

答案 0 :(得分:0)

我注意到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用作多维数组。我觉得这样做更好,这样你就不会覆盖键/值对。