CakePHP - 如何允许未经身份验证的访问特定页面 [英] CakePHP - How to allow unauthenticated access to specific pages

查看:232
本文介绍了CakePHP - 如何允许未经身份验证的访问特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CakePHP应用程序,其中我创建了一个 UsersController ,它处理所有关于用户。
当我尝试浏览 www.mydomain.com 时,如果我已经登录,它让我看到索引( app / View /Pages/home.ctp )。否则,它会将我重定向到 mydomain.com/users/login ,并且仍然登录。



查看 AppController.php PagesController.php app / Config / core.php app / Config / routes.php ,但没有找到任何东西。



我不记得了,我也没有记住。找不到如何禁用这个。



编辑:我的CakePHP版本是2.3。

解决方案

一般来说,您可以使用 auth components allow() method



因为 PagesController 在一个操作( display())中处理所有页面,所以需要多做一些工作, )。如果是这样,那么您可以使用 request-> params ['pass'] [0] ,它将保存页面名称,页面,然后使用 Auth :: allow 允许显示操作。

例如,在 PagesController 中:

  beforeFilter()
{
parent :: beforeFilter();

$ allowedPages = array('home','foo','bar');
if(isset($ this-> request-> params ['pass'] [0])&&
in_array($ this-> request-> params ['pass '] [0],$ allowedPages))
{
$ this-> Auth-> allow('display');
}
}

这将允许页 home foo bar

如果您想将所有网页设为公开,那么您只需使用 Auth :: allow 没有任何条件,即:

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


I have created a CakePHP app where I have created a UsersController, which handles all about users. When I try to browse www.mydomain.com, if I am logged in, it let's me see the index (app/View/Pages/home.ctp). Else, it redirects me to mydomain.com/users/login and persists to log in.

I have tried looking at AppController.php, PagesController.php or app/Config/core.php and app/Config/routes.php, but did not find anything. My UsersController.php, also, is not responsible for that, I think.

I do not remember and I cannot find how to disable this. Which file should be responsible for that?

EDIT:my CakePHP version is 2.3.

解决方案

Generally you can make specific actions public using the auth components allow() method.

Making pages public may require a little more work in case you'd want to make only specific pages public, since the PagesController handles all pages in a single action (display()). If that is the case, then you could utilize request->params['pass'][0] which will hold the page name, test this against a list of allowed pages, and then allow the display action using Auth::allow.

Example, in the PagesController:

public function beforeFilter()
{
    parent::beforeFilter();

    $allowedPages = array('home', 'foo', 'bar');
    if(isset($this->request->params['pass'][0]) &&
       in_array($this->request->params['pass'][0], $allowedPages))
    {
        $this->Auth->allow('display');
    }
}

This would allow the pages home, foo and bar to be viewed without being logged in.

If you'd wanted to make all pages public, then you could simply use Auth::allow without any conditions, ie:

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

这篇关于CakePHP - 如何允许未经身份验证的访问特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆