AuthComponent 在某些控制器中不可用?

发布于 2025-01-07 00:08:21 字数 1155 浏览 1 评论 0原文

我已经编写了我的蛋糕应用程序来登录注册用户,并且它在存在与用户关联的数据库模型的视图页面上运行得很好。然而,在我的主页上,不一定要访问某些模型(PagesController,其中页面是即将发生的事件、联系我们、关于等),AuthComponent 不可用,或者至少,数组返回空---所以我无法检索登录者的用户名。

我尝试创建一个名为 Page 的模型,该模型属于用户,但这并没有解决我的问题。

为了进一步解释一下,我的应用程序显示了特定城市的某些住宿、夜总会、餐馆和活动的列表,无论用户是否登录,所有这些都会显示。我不明白我错在哪里以及为什么。

这是我的 AppController:

<?php
class AppController extends Controller {
 public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'view'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'index')
    )
);


function beforeFilter() {
    $this->Auth->allow('login', 'logout','index', 'view', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us');

}
}
?>

这是我在默认页面布局中访问我的用户名的位置:

<?php if(AuthComponent::user('id')) {
        echo '<span style="font-variant:small-caps">Hello, '.AuthComponent::user('username').'</span> | ';
?>

I have written my cake app to log in registered users and it works great on view pages where there is a db model associated with a User. However, on my main pages that aren't necessarily accessing some model (the PagesController where pages are things like upcoming events, contact us, about, etc), AuthComponent is not available, or at the least, the array returns empty---so i cannot retrieve, say, the username of the person logged in.

I tried creating a model called Page that belongsTo User but that didn't fix my problem.

To explain a little further, my app shows lists of certain lodgings, nightclubs, restaurants and things to do for a given city, all of which is shown whether a user is logged in or not. I don't understand where I am going wrong and why.

Here is my AppController:

<?php
class AppController extends Controller {
 public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'view'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'index')
    )
);


function beforeFilter() {
    $this->Auth->allow('login', 'logout','index', 'view', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us');

}
}
?>

here is where I access my username in my default page layout:

<?php if(AuthComponent::user('id')) {
        echo '<span style="font-variant:small-caps">Hello, '.AuthComponent::user('username').'</span> | ';
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2025-01-14 00:08:21

经过几周的努力,我找到了答案。哦,你会笑的。对于任何遇到这个问题的人来说,答案是……确保标签前后没有空格。仅此而已。严重地。由于 PHP 打印出 PHP 标记中没有的所有内容,因此如果它找到要发送的内容,它将在其他内容之前发送标头。一旦发送了标头,就无法创建 PHP 会话。所以把这个记为令人头疼的文件吧!

I have found the answer, after weeks of beating my head on the wall about this. And oh, you're gonna laugh. For any of you out there up against this problem, the answer is.....make sure there is no white space before or after your tags. That is all. Seriously. Since PHP prints out everything that isn't in a PHP tag, if it finds something to send, it will send out headers before everything else. Once headers are sent, a PHP session cannot be created. So chalk this one up to the head-smacking file!

温柔戏命师 2025-01-14 00:08:21

在没有看到任何代码的情况下,我的第一直觉是你没有在 AppController 中加载组件,而是仅在 UsersController 中加载该组件。您是否在 AppController 中加载 Auth 组件?

更新:

以下是我建议访问用户信息的方法。

$this->Session->read('Auth.User.username');

更新 2

从页面控制器中删除 $components 声明。它可能会覆盖会话。

更新3

如果您有自定义页面控制器,它可能与核心页面控制器冲突。删除 Pages 控制器并查看其是否正常工作。 Core Pages 控制器仍将路由到 Views/Pages/ 目录来显示内容。

Without seeing any code my first instinct is to say that you are not loading the component in AppController but in the UsersController only. Are you loading the Auth component in AppController?

UPDATE:

Here is how I would suggest accessing the User information.

$this->Session->read('Auth.User.username');

UPDATE 2

Remove the $components declaration from the Pages Controller. It could be overriding the session.

UPDATE 3

If you have a custom Pages controller, it is probably conflicting with the Core Pages controller. Remove your Pages controller and see if it works correctly. The Core Pages controller will still route to the Views/Pages/ directory for displaying content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文