CakePHP Auth 如何允许特定的控制器和操作
我有一个“帖子”和一个“用户”控制器。我使用身份验证组件,我希望所有用户都可以访问“Post.index”,但只有登录的用户才能访问“User.index”。
在我的 app_controller.php 中,我有这个
$this->Auth->allow('signup', 'confirm', 'index');
,但所有用户都可以访问 post.index 和 user.index。如何在允许方法中指定控制器?
这对我不起作用:
$this->Auth->allow('signup', 'confirm', 'Post.index');
更新 我从 app_controller.php 中删除了“index”,并将其设置在后控制器的 beforeFilter 方法中:
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('index');
}
我还在 app_controller 中设置了一个变量“loggedIn”,而不调用“parent::beforeFilter();”我收到“未定义变量”通知。
谢谢 sibidiba
I have a "Posts" and a "Users" controller. I use the Auth Component and I want that all users can visit "Post.index" but only logged in users can visit "User.index".
In my app_controller.php I have this
$this->Auth->allow('signup', 'confirm', 'index');
but with that all users can visit post.index and user.index. How can I specify a Controller in the allow-method?
This didn't work for me:
$this->Auth->allow('signup', 'confirm', 'Post.index');
update
I removed 'index' from the app_controller.php and instead set it in the beforeFilter method in the post controller:
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('index');
}
I also set a variable "loggedIn" in app_controller, without calling "parent::beforeFilter();" I got an "undefined variable" notice.
thx sibidiba
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
期间不会工作。你可以尝试用“/”代替。如果同样失败,您应该在 PostController 和 UserController 的
::beforeFilter()
中分别设置$this->Auth->allow('index')
。不要忘记调用parent::beforeFilter()。The period will not work. You could try '/' instead. If that fails as well, you should set
$this->Auth->allow('index')
in PostController's and UserController's::beforeFilter()
individually. Don't forget to call parent::beforeFilter().取决于您正在处理的版本。如果是 cakephp 2.x,请将此代码放入具有您想要无需登录即可访问的操作的控制器中。作为您的问题,您应该将此代码放入 Posts 控制器:
allow(array('action you want to allowed'))
而不是allow('action you want to allowed')
Depends on the version you're working on. If it's cakephp 2.x, put this code into the controller that has the action you want give access without login. As your question, you should put this code to Posts controller:
allow(array('acction you want to allow'))
insteadallow('acction you want to allow')
我正在使用 CakePHP 2.x。斜线技巧不起作用。
如果您想允许用户无需登录即可访问“myController.myAction”,您应该将 beforeFilter() 添加到 myController.php 而不是 AppController.php
以下是添加到 myController.php 的代码:
I am using CakePHP 2.x. The slash trick doesn't work.
If you want to allow user access "myController.myAction" without login, you should add beforeFilter() into myController.php instead of AppController.php
Here is the code to add into myController.php:
对于 Cakephp 2.x,有几种方法(取决于 cakephp 版本)。
从文档(http://book.cakephp.org/2.0 /en/core-libraries/components/authentication.html):
For Cakephp 2.x, there are several methods (depending on the cakephp version).
From the docs (http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html):
对于 CakePHP 开发人员来说,授权允许特定控制器的特定操作是一个常见问题
https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/
It’s a common problem to CakePHP developer to auth allow to specific actions of a specific controller
https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/
在 cake 3.x 中,您可以使用以下代码行来允许所有操作。
In cake 3.x you can use below lines of code to allow all the actions.
$this->name 返回当前请求的控制器。
在 AppController::beforeFilter() 中试试这个
抱歉,我的英语不好
$this->name returns current Controller requested.
try this in AppController::beforeFilter()
Sorry, my english is not good
对于CakePHP 3.*,在特定控制器中允许特定方法
For CakePHP 3.* to allow specific methods in the specific controller