CakePHP - 如何允许未经身份验证的访问特定页面

CakePHP - How to allow unauthenticated access to specific pages(CakePHP - 如何允许未经身份验证的访问特定页面)
本文介绍了CakePHP - 如何允许未经身份验证的访问特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

我曾尝试查看 AppController.phpPagesController.phpapp/Config/core.phpapp/Config/routes.php,但没有找到任何东西.我的 UsersController.php 也不对此负责,我想.

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?

我的 CakePHP 版本是 2.3.

my CakePHP version is 2.3.

推荐答案

通常,您可以使用 auth 组件allow() 方法.

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

如果您只想公开特定页面,则公开页面可能需要做更多工作,因为 PagesController 在单个操作中处理所有页面 (display()).如果是这种情况,那么您可以使用 request->params['pass'][0] 来保存页面名称,针对允许的页面列表进行测试,然后允许使用 Auth::allowdisplay 动作.

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.

示例,在 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');
    }
}

这将允许页面 homefoobar 无需登录即可查看.

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

如果您想让所有页面公开,那么您可以简单地使用 Auth::allow 而不带任何条件,即:

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 - 如何允许未经身份验证的访问特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)