如何从cakephp .ctp文件中设置会话消息

时间:2014-04-21 14:41:52

标签: javascript php jquery session cakephp

我在视图页面上有一个按钮,因为我必须检查谁在点击该按钮我们在我们的网站上有不同类型的用户,因此我们不希望允许我们网站的特定类型的用户点击它或获得任何更改,以便我们在点击时点击谁点击按钮是我们想要的用户或我们不想要的人,因为我在按钮点击时检查会话ID 。 那么现在我想要的是,如果我们不想要的用户点击该按钮,那么他们应该重定向到带有会话消息的感谢页面。 我知道如何从cakephp控制器显示会话消息的方法,我使用了这个:

**$sending = SITE_URL."thanks";
            $this->Session->setFlash(__("Sorry! You are unauthorized user. Contact us at help@example.com", true), 'default', array('class' => 'notclass'));
            echo "<script type='text/javascript'> window.location.href='".$sending."';</script>";
            die;**

但是当我在条件之后在.ctp文件中尝试相同的逻辑时 if($this->Session->read('Auth.User.id')!='' && $this->Session->read('Auth.User.role_id')=='2'){

<th></br><span style="font-family: 'proximanova semibold',Ubuntu;font-weight: normal; color: #333333;font-size: 18px;">Basic</span><br/><span>Free</span><br/>
        <div class="apply-button-search" style="float: none; margin: auto; margin-bottom: 5px;">

    <input class="profilebtn" type="button" style="width:100px !important; margin-top: 1px;" value="Start now" onclick="window.location.href='<?php e(SITE_URL."thanks");?>'">
<?php $sending = SITE_URL."thanks";
                $this->Session->setFlash(__("Sorry! You are unauthorized user. Contact us at help@example.com", true), 'default', array('class' => 'notclass'));
                echo "<script type='text/javascript'> window.location.href='".$sending."';</script>";
                die;?>

我收到此错误 Fatal error: Call to undefined method SessionHelper::setFlash() in /var/www/html/example/app/webroot/testdemo/app/views/fronts/abc.ctp on line 123 所以我只想知道如何使用此会话帮助程序逻辑从我的.ctp文件中设置flash消息。 感谢

2 个答案:

答案 0 :(得分:0)

setFlashSessionComponent的一种方法,用于Controller。您可以在视图上使用组件,但最好使用$this->Session->flash();,因此flash()SessionHelper的方法。

答案 1 :(得分:0)

如果您非常需要,可以使用CakeSessionis used in Session component)。

有更好的方法来处理这类问题。几个例子:

  • prefix routing
  • 每个组/角色的不同视图
  • 包含菜单/按钮组/ ...的每个组/角色的不同元素