这是我的AdminsController
类,其中包含了Cookie组件,并使用了下面#1中给出的cookie创建代码。 #2是我在我看来使用的代码。
每当我触发网址时,都会显示错误:"Helper class CookieHelper could not be found."
。
为什么会显示此错误,因为我已要求使用CookieComponent
设置并获取Cookie的值。我已按照他们在其网站上提供的 CakePHP 文档中的说明和代码语法。我在设置和获取cookie时遇到了麻烦。我需要帮助。我正在使用的 CakePHP 版本是2.6。
#1 - 控制器:
class AdminsController extends AppController {
/**
* This controller does not use a model
*
* @var array
*/
//public $uses = array('Admin');
/**
* Displays a view
*
* @return void
* @throws NotFoundException When the view file could not be found
* or MissingViewException in debug mode.
*/
public $components = array('Cookie');
public function index() {
$this->layout=false;
//echo "=users index====";
//$this->layout = 'adminlayout';
if($this->request->is('post'))
{
$username = $this->request->data['Admin']['username'];
$password = $this->request->data['Admin']['password'];
$remember_me = $this->request->data['Admin']['remember_me'];
$sql = $this->Admin->find('count',array(
'conditions' => array('Admin.username'=>$username,'Admin.password'=>md5($password))
));
//$this->Session->write('is_admin_login','Y');
if($sql>0)
{
$this->Session->write('is_admin_login','Y');
if(!empty($remember_me))
{
//*** set cookie starts
if($this->Cookie->check('username_cookie'))
{
$this->Cookie->delete('username_cookie');
$this->Cookie->write('username_cookie', $username, false, '30 Days');
}
if($this->Cookie->check('password_cookie'))
{
$this->Cookie->delete('password_cookie');
$this->Cookie->write('password_cookie', $password, false, '30 Days');
}
//*** set cookie ends
}
else
{
if($this->Cookie->check('username_cookie'))
{
$this->Cookie->delete('username_cookie');
}
if($this->Cookie->check('password_cookie'))
{
$this->Cookie->delete('password_cookie');
}
}
$this->redirect('/admin/home');
}
else
{
$this->Session->setFlash('Invalid username or password','default',array('class'=>'error_msg'));
}
}
$this->render('admin_login');
}
}
#2 - AdminLogin查看
<div class="form-box" id="login-box">
<div class="header">Sign In</div>
<?php echo $this->Form->create(null, array(
'url' => array('controller' => 'Admins', 'action' => 'index')
)); ?>
<div class="body bg-gray">
<div class="row">
<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
<?php echo $this->Session->flash(); ?>
</div>
</div>
<div class="form-group">
<?php /*?> <input type="text" name="user_name" id="user_name" class="form-control" placeholder="User ID" value="<?php // echo get_cookie("username_cookie"); ?>"/><?php */?>
<?php
$options=array();
$options['id']='username';
$options['class']='form-control';
$options['placeholder']='User ID';
$options['label']=false;
if($this->Cookie->check('username_cookie'))
{
$options['value']=$this->Cookie->read('username_cookie');
}
echo $this->Form->input('username',$options);
?>
</div>
<div class="form-group">
<?php /*?><input type="password" name="password" id="password" class="form-control" placeholder="Password" value="<?php // echo get_cookie("password_cookie"); ?>"/><?php */?>
<?php
$options=array();
$options['id']='password';
$options['class']='form-control';
$options['placeholder']='Password';
$options['label']=false;
if($this->Cookie->check('password_cookie'))
{
$options['value']=$this->Cookie->read('password_cookie');
}
echo $this->Form->input('password',$options);
?>
</div>
<div class="form-group">
<?php
$options=array();
$options['id']='remember_me';
//$options['class']='form-control';
$options['type']='checkbox';
$options['label']=false;
$options['div']=false;
$checked="";
if($this->Cookie->check('username_cookie') || $this->Cookie->check('password_cookie'))
{
$options['checked']="checked";
}
echo $this->Form->input('remember_me', $options);
?> Remember me
</div>
</div>
<div class="footer">
<button type="submit" class="btn bg-olive btn-block">Sign me in</button>
<p><a href="<?php // echo base_url()."admin/login/forgot"; ?>">Forgot my password</a></p>
</div>
<?php
echo $this->Form->end();
?>
<div class="margin text-center">
<!-- <span>Sign in using social networks</span>-->
<br/>
</div>
答案 0 :(得分:0)
您正在尝试在视图中使用Component,这就是为什么它告诉您没有名为Cookie
的帮助程序类。
使用Controller操作中的Cookie组件设置变量,然后在视图中使用该变量:
从视图中删除这些行:
$this->Cookie->check('username_cookie')
$this->Cookie->check('password_cookie')
在控制器操作中使用它们来设置视图变量......类似于:
$this->set('username_cookie', $this->Cookie->read('username_cookie'));
$this->set('password_cookie', $this->Cookie->read('password_cookie'));
然后,您将能够在视图中使用设置变量:
if(!empty($username_cookie)) {
//do something
}
//or
if(!empty($password_cookie)) {
//do something
}