簡體   English   中英

我如何將用戶重定向到用戶頁面並將管理員重定向到 cakephp 中的管理頁面

[英]how can i redirect user to the user page and admin to the admin page in cakephp

每當我登錄時,我都會將所有內容重定向到“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>

您是否在 controller 中設置您的用戶類型? 例如,如果您有一個名為 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'));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM