我還在學習cakephp。 我做了一個畫廊,所以每一頁都是公開的,我沒有爲用戶帳戶,但有一個管理員帳戶發佈的照片/視頻/工作我v搜索,但無法找到符合我的情況。 這裏是我的AppController的cakephp的授權
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array('Auth' => array(
'loginAction' => array(
'controller' => 'admins',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'display','adminpanel'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authError' => 'Did you really think you are allowed to see that?',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'Username','password'=>'password')
)
)
),'DebugKit.Toolbar');
public function beforefilter(){
$this->Auth->authenticate = array('Form');
}
}
?>
這是我的AdminsController
<?php
App::uses('AppController', 'Controller');
class AdminsController extends AppController {
public $uses='Admin';
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Paginator','Session');
//this can be changed later
//if the system has users and admins
public function login()
{
$this->layout = 'login';
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
?>
而且也對管理員/ login.ctp
<?php echo $this->Form->create('Admin',array('class'=>'form-signin')); ?>
<h2 class="form-signin-heading"><?php echo __('Please sign in'); ?></h2>
<?php echo $this->Form->input('Username',array('type'=>'text','placeholder'=>'Username','class'=>'form-control'));?>
<?php echo $this->Form->input('password',array('type'=>'password','placeholder'=>'Password','class'=>'form-control'));?>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<?php echo $this->Form->end(array('value' => 'Sign in', 'class' => 'btn btn-lg btn-primary btn-block'));
現在,當我去網站它重定向我的管理員登錄,我不想這樣。 我只是想查看的網站作爲一個公衆觀衆,如果我提前
堅持約定。一開始會讓你更容易。您的代碼包含一些違規和缺陷,例如'input('Username'' => db中的字段名稱應該是小寫字母和snake_case。$ uses是不必要的,因爲它會自動使用單數Admin - 此外,這個字段通常是一個數組。 – mark