我如何将用户重定向到用户页面并将管理员重定向到 cakephp

时间:2021-04-30 07:09:45

标签: php cakephp cakephp-4.x

每当我登录时,我都会将所有内容重定向到“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>

1 个答案:

答案 0 :(得分:0)

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

 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'));
}
相关问题