简体   繁体   English

允许静态页面通过CakePHP登录

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

I have a static page I want to add to the existing cakePHP project. 我有一个静态页面想要添加到现有的cakePHP项目中。 I managed to get around the Auth through using this code on PagesController 我设法通过在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();
    }
}

And added the route like this: 并添加如下路线:

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

But what's happening is that when a user logs in, the login page displays again. 但是发生的是,当用户登录时,再次显示登录页面。 I think a validation should be added to check if a user is logged in here: 我认为应该添加验证以检查用户是否在此处登录:

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

How can I do this? 我怎样才能做到这一点?

I tried these answers: Auth for static page 我尝试了以下答案: 对静态页面进行身份验证

Allowing a Specific Page in Cakephp 在Cakephp中允许特定页面

I don't think it's necessary to go through so much hassle. 我认为不必经历那么多麻烦。 For eg: If the name of your action is "privacyPolicy", you could simply specify it within $this->Auth->allow() in AppController itself. 例如:如果操作的名称是“ privacyPolicy”,则可以在AppController本身的$ this-> Auth-> allow()中简单地指定它。

In case you'd like to keep it separated and write it within PagesController, I'd suggest you to call the parent function. 如果您想使其分开并在PagesController中编写它,建议您调用父函数。 Otherwise, the beforeFilter within PagesController overrides the beforeFilter of AppController. 否则,PagesController中的beforeFilter会覆盖AppController的beforeFilter。

   //AppController.php

  /* Other code */

  public function beforeFilter() {

  ..........

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

_____________________ OR ________________________________ _____________________ 要么​​ ________________________________

  // PagesController.php

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

Hope this helps. 希望这可以帮助。

Peace! 和平! xD xD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM