如何在cakepp中将用户重定向到用户页面,将管理员重定向到管理页面



每当我登录时,我都会将所有重定向到"welcome.php"。但如果我希望用户在用户页面上重定向,而管理员在管理员页面上重定向。我能做什么?我已经定义了用户的类型,只想单独重定向它们。例如,当一个新用户注册/注册时,用户和管理员选项就在那里。

在UsersController.php 中

public function login()
{
$this->viewBuilder()->setLayout('login');
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
if ($result->isValid()) {
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Users',
'action' => 'welcome',
]);

return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}

}

注册.php

<label for="utype" class="text-gray-600  mb-2 "> 
Usertype</label>
<div class="form-check ">
<label class="radio-inline">
<input type="radio" name="utype" value="admin"  
class=" px-3 py-2  border border-gray-300 rounded-md 
" />;
Admin </label>
<label class="radio-inline">
<input type="radio" name="utype" value="user"   
class="px-3 py-2  border border-gray-300 rounded-md"  
checked/>;
User </label>
</div>

</div>

您是否在控制器中设置用户类型?例如,如果您有一个名为user-type的属性,则可以检查其值并设置"$动作";从那里

public function login(){
$this->viewBuilder()->setLayout('login');
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
if ($result->isValid()) {
$action = $this->user_type=='admin'?'admin':'welcome'
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Users',
'action' => $action,
]);
return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}

最新更新