Cakephp 避免重定向并支持会话

Cakephp 避免重定向并支持会话,cakephp,Cakephp,我正在尝试用cake php来完成这个网站,它以前是普通的php,但是作为cakephp世界的新手。。我发现了一些这样的困难 1.当我单击登录时,它会将我转移到另一个页面,尽管我在app controller中未指定重定向,但有注册时除外。登录名位于顶部,可在所有页面中查看,但如果单击“登录”,它会将我重定向到我不想要的登录页面 二,。在我登录之后,它会带来用户名为welcome‘username’的会话,但如果我转到另一个页面,它似乎忘记了会话,并在页面顶部返回登录表单的输入 这是我的密码 应

我正在尝试用cake php来完成这个网站,它以前是普通的php,但是作为cakephp世界的新手。。我发现了一些这样的困难

1.当我单击登录时,它会将我转移到另一个页面,尽管我在app controller中未指定重定向,但有注册时除外。登录名位于顶部,可在所有页面中查看,但如果单击“登录”,它会将我重定向到我不想要的登录页面

二,。在我登录之后,它会带来用户名为welcome‘username’的会话,但如果我转到另一个页面,它似乎忘记了会话,并在页面顶部返回登录表单的输入

这是我的密码

应用程序控制器

<?php
class AppController extends Controller {    

    var $helpers = array('Html', 'Form', 'Javascript', 'Session'); 
    var $components = array('Auth', 'Session');

    function beforeFilter() {
       $this->Auth->allow('add','get_categories','get_home', 'get_others', 'pages');
       $this->Auth->autoRedirect = false;

      } 
}
?>
<?php
class AppController extends Controller {    

    var $helpers = array('Html', 'Form', 'Javascript', 'Session'); 
    var $components = array('Auth', 'Session');

    function beforeFilter() {
       //new addition
       $this->set('userData', $this->Session->read());
       $this->Auth->allow('add','get_categories','get_home', 'get_others', 'pages', '*');
       $this->Auth->autoRedirect = false;

      } 
}
?>
但是当我浏览到一个新的页面时

在元素/loginform中添加了PRINT\R以查看所有页面的可变内容


1这是因为用户登录后,在UsersController中有一个重定向。 您的登录函数应改为:

 function login() {

        $this->layout = 'master_layout';

              if ($this->data) {

                    if ($this->Auth->login($this->data)) {

                    // Retrieve user data
         $results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false);

/*This is the offending line, I've commented it out, but you could have it redirect somewhere else (it might be a good idea to redirect to the index action, for example, or just delete it:*/

         //$this->redirect(array('controller' => 'users', 'action' => 'login'));


                            }
                    }

        $this->data['User']['password'] = '';

    }
至于2,我认为这可能是因为您没有在事后设置数据,因此,当数据存在于使用会话组件的服务器上的tmp目录中时,它实际上没有被传递到视图。所以基本上我认为这是因为不能在视图中调用会话方法。即使我对此不是完全正确,我建议的做法也应该有效:不要在视图中调用会话方法,而是在控制器中调用它们,使用$this->set;,在视图要访问的变量中设置它们;,然后在视图中针对该变量进行测试

如果你需要这么多的视图和动作,你可以考虑把这样的东西添加到你的控制器甚至你的应用程序控制器:

function beforeFilter() {
// Get session data for appllication use
$this->appuserstuff = $this->Session->read();
} 

function beforeRender() {
// Make app variables available to view
$this->set('userData', $this->appuserstuff);
}
或者,如果您只需要在几个操作中使用此选项,您可以使用以下选项设置这些操作中的用户数据:

$this->set('userData', $this->Session->read());
现在,我建议您在其中一个视图中执行一个测试,以便在设置数据时可以看到数据在数组中的结构,从而可以使用或测试条件。最后,您可以将视图中对会话的直接调用替换为对数据数组的检查:

请注意,我不确定您的特定阵列是如何构造的,因此请按照上面的建议进行调试,并插入您自己的密钥以使其工作:


好的,我会做更多的研究,然后再告诉你当你不做$this->Session->readyoudo$this->Auth->user时会发生什么;在你的appcontroller中?我尝试了上面的代码。。现在表单没有更改为“欢迎名称”,而是保留两个文本框以供输入,但数组中有数据,但再次,当我移动到另一个页面时,数组将重置。。我要试试这个->身份验证->用户..好的,我添加了$this->auth->user;现在它将文本框切换到欢迎名称。。但当我浏览到另一个页面时,它仍然会清除整个页面。。并再次显示这两个文本框。。哦,天哪,我能用它吗$_也许在什么地方?
Array ( [Config] => Array ( 
    [userAgent] => 8f12200c2d48fa7955465842befe1c9e 
    [time] => 1323562591 [timeout] => 10 ) ) 
<?php
 print_r($userData);
 if ($this->Session->read('Auth.User.username')):?>
<?php
  echo "Welcome".' ' ;
echo $this->Session->read('Auth.User.username');
echo " ";
echo $html->link('logout', array('action'=>'logout'));
 ?>
<?php else : ?>
<div class="types form">
      <?php echo $form->create('User', array('controller' => 'Users','action' => 'login')); ?>
      <?php echo $form->input('username', array('label' => 'username'));    ?>
      <?php echo $form->input('password',array('type'=>'password', 'label' => 'password')) ?>
      <?php echo $form->submit('Submit'); ?>
       </div>
 <?php endif; ?> 
 function login() {

        $this->layout = 'master_layout';

              if ($this->data) {

                    if ($this->Auth->login($this->data)) {

                    // Retrieve user data
         $results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false);

/*This is the offending line, I've commented it out, but you could have it redirect somewhere else (it might be a good idea to redirect to the index action, for example, or just delete it:*/

         //$this->redirect(array('controller' => 'users', 'action' => 'login'));


                            }
                    }

        $this->data['User']['password'] = '';

    }
function beforeFilter() {
// Get session data for appllication use
$this->appuserstuff = $this->Session->read();
} 

function beforeRender() {
// Make app variables available to view
$this->set('userData', $this->appuserstuff);
}
$this->set('userData', $this->Session->read());
<?php
 if (!empty($userData['User'])):?>
<?php
  echo "Welcome".' ' ;
echo $userData['User']['username'];
echo " ";
echo $html->link('logout', array('action'=>'logout'));
 ?>
<?php else : ?>
<div class="types form">
      <?php echo $form->create('User', array('controller' => 'Users','action' => 'login')); ?>
      <?php echo $form->input('username', array('label' => 'username'));    ?>
      <?php echo $form->input('password',array('type'=>'password', 'label' => 'password')) ?>
      <?php echo $form->submit('Submit'); ?>
       </div>
 <?php endif; ?>