我在从admin部分提交表单时遇到了不确定的问题。我在routes.php中使用前缀是routes.php的前缀代码
Router::prefix('admin', function ($routes) {
// Because you are in the admin scope,
// you do not need to include the /admin prefix
// or the admin route element.
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
//$routes->connect('/admin', ['controller' => 'Users', 'action' => 'index']);
// And have the prefix => admin route element added.
$routes->fallbacks(DashedRoute::class);
});
下面是我的app控制器授权码: -
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'index',
'plugin' => null
],
//'authorize' => ['Controller'],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'dashboard'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'index',
''
],
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password']
]
],
'authError' => 'Did you really think you are allowed to see that?',
'storage' => 'Session'
]);
以下是主要的用户控制器功能: -
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
$this->loadComponent('RequestHandler');
$this->Auth->allow(['index','logout','register']);
}
默认情况下,用户控制器登录功能设置索引: -
public function index()
{
$this->viewBuilder()->layout('adminlogin');
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user){
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}else{
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
最后,这是注册用户,当我提交注册表单时,它会在数据库中成功保存数据,然后重定向到BASEURL / webroot / admin
public function register()
{
$this->viewBuilder()->layout('adminlogin');
$this->loadModel('Users');
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['controller' => 'Users','action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$user = $this->Users->newEntity();
$this->set('user', $user);
}
用户是登录表单操作: -
<?php echo $this->Form->create(null, ['url' => ['controller' => 'Users', 'action' => 'index']]); ?>