簡體   English   中英

允許靜態頁面通過CakePHP登錄

[英]Allowing a static page get through login in cakePHP

我有一個靜態頁面想要添加到現有的cakePHP項目中。 我設法通過在PagesController上使用此代碼來繞過Auth

public $allowedPages = array('main',); 


public function beforeFilter() {
$this->Auth->allow('display');
}
public function display()
{
    $path = func_get_args();

    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    $this->set(compact('page', 'subpage'));

    /*add CHU
    if(in_array($page, $this->allowedPages) || $this->User->loggedin) {
    $this->render($page);
    } */

    if(in_array($page, $this->allowedPages) ) {
        $this->render($page); //here redirects to login page change the path if the path is different
    }


    try {
        $this->render(implode('/', $path));
    } catch (MissingTemplateException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}

並添加如下路線:

$routes->connect('/main', ['controller' => 'Pages', 'action' => 'display', 'main']);

但是發生的是,當用戶登錄時,再次顯示登錄頁面。 我認為應該添加驗證以檢查用戶是否在此處登錄:

if(in_array($page, $this->allowedPages) ) {
        $this->render($page); //here redirects to login page change the path if the path is different
    }

我怎樣才能做到這一點?

我嘗試了以下答案: 對靜態頁面進行身份驗證

在Cakephp中允許特定頁面

我認為不必經歷那么多麻煩。 例如:如果操作的名稱是“ privacyPolicy”,則可以在AppController本身的$ this-> Auth-> allow()中簡單地指定它。

如果您想使其分開並在PagesController中編寫它,建議您調用父函數。 否則,PagesController中的beforeFilter會覆蓋AppController的beforeFilter。

   //AppController.php

  /* Other code */

  public function beforeFilter() {

  ..........

  $this->Auth->allow(array(
     "action1",
     "action2",
     "display"
   ));
  }

_____________________ 要么​​ ________________________________

  // PagesController.php

   public function beforeFilter() {
      parent::beforeFilter(); // Add this line
      $this->Auth->allow('display');
   }

希望這可以幫助。

和平! xD

暫無
暫無

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

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