允许静态页面通过cakePHP登录

允许静态页面通过cakePHP登录,php,authentication,cakephp,cakephp-3.x,Php,Authentication,Cakephp,Cakephp 3.x,我有一个静态页面要添加到现有的cakePHP项目中。通过在PagesController上使用此代码,我成功地绕过了身份验证 public $allowedPages = array('main',); public function beforeFilter() { $this->Auth->allow('display'); } public function display() { $path = func_get_args(); $count = co

我有一个静态页面要添加到现有的cakePHP项目中。通过在PagesController上使用此代码,我成功地绕过了身份验证

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
    }
我该怎么做

我试过这些答案:


我认为没有必要经历这么多麻烦。例如:如果操作的名称是“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

我认为没有必要经历这么多麻烦。例如:如果操作的名称是“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

通过您在那里所做的,您将一起绕过身份验证,这不是一个非常好的主意。尝试通过auth组件允许特定页面:您的登录页面的url和操作是什么?它是写在PagesController中的吗?@ndm,我在你推荐的答案中添加了这个类。我在未加载页面的情况下收到以下错误和警告:
Strict(2048):App\Controller\PagesController::beforeFilter()的声明应与App\Controller\AppController::beforeFilter(Cake\Event\Event$Event)[App/Controller/PagesController.php,第29行]警告(4096):传递给App\Controller\AppController::beforeFilter()的参数1必须是Cake\Event\Event的实例,未给定,在第34行的/var/www/sty_reps/src/Controller/PagesController.php中调用并定义[APP/Controller/AppController.
@ObjectManipulator,登录页面的url为sitename/login。sitename/还重定向到/login。这些在路由中定义,而不是PageController@YohanBlake您问题中的代码示例将失败,并出现相同的错误。对于以后的问题,请始终提及您的确切CakePHP版本和标记相应地,您的问题!错误消息非常清楚,您需要使用兼容的签名,并相应地调用父方法。通过您在那里所做的操作,您将全部绕过身份验证,这不是一个非常好的主意。请尝试通过auth组件允许特定页面:您登录的url和操作是什么年龄?它是写在PagesController中的吗?@ndm,我在你推荐的答案中添加了这个类。我在没有加载页面的情况下得到了以下错误和警告:
Strict(2048):App\Controller\PagesController::beforeFilter()的声明应该与App\Controller\AppController::beforeFilter(Cake\Event\Event$Event)兼容[APP/Controller/PagesController.php,第29行]警告(4096):传递给APP\Controller\AppController::beforeFilter()的参数1必须是Cake\Event\Event的实例,未给定,在第34行的/var/www/sty_reps/src/Controller/PagesController.php中调用并定义[APP/Controller/AppController.
@ObjectManipulator,登录页面的url为sitename/login。sitename/还重定向到/login。这些在路由中定义,而不是PageController@YohanBlake您问题中的代码示例将失败,并出现相同的错误。对于以后的问题,请始终提及您的确切CakePHP版本和标记您的问题相应!错误消息非常清楚,您需要使用兼容的签名,并相应地调用父方法。AppController中的编辑工作正常,但现在的问题是
mysite/
没有重定向到/login页面。这可能是因为包含了
“display”
在允许数组中?这是应该执行路由的路由文件中的代码:
$routes->connect(“/”、['controller'=>'Pages','action'=>'display','home']);
基本上,通过执行您提到的操作,如果用户登录,它将非常有效。但是如果用户未登录,mysite/不会重定向到mysite/login@ObjectManipulatorpages controller通过单个操作为所有页面提供服务,因此通过无条件地允许
显示
操作,您将允许访问所有页面,而ich就是为什么需要检查请求页面的麻烦。AppController中的编辑工作正常,但现在的问题是
mysite/
没有重定向到/login页面。这可能是因为包含了
“display”
在允许数组中?这是应该执行路由的路由文件中的代码:
$routes->connect(“/”、['controller'=>'Pages','action'=>'display','home']);
基本上,通过执行您提到的操作,如果用户登录,它将非常有效。但是如果用户未登录,mysite/不会重定向到mysite/login@ObjectManipulatorpages controller通过单个操作为所有页面提供服务,因此通过无条件地允许
显示
操作,您将允许访问所有页面,而这就是为什么检查请求页面的麻烦是必要的。