【问题标题】:How can Set session message from cakephp .ctp file如何从 cakephp .ctp 文件设置会话消息
【发布时间】:2014-04-21 14:41:51
【问题描述】:

您好,我在查看页面上有一个按钮,我必须检查谁在点击该按钮我们的网站上有不同类型的用户,所以我们不想允许我们网站的特定类型的用户单击该按钮或进行任何更改,以便我们在单击时检查谁在单击按钮是我们想要或不想要的用户,因此我在单击按钮时检查会话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-&gt;Session-&gt;read('Auth.User.id')!='' &amp;&amp; $this-&gt;Session-&gt;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 文件中设置闪存消息。 谢谢

【问题讨论】:

  • 您应该了解SessionComponentSessionHelper 之间的区别。 ComponentsController 的扩展,HelpersView 的扩展。因此,在您查看文件中,您实际上是在调用SessionHelper,而setFlash 不是SessionHelper 的方法。
  • 感谢@FazalRasel 的回复让我看看 SessionHelper 和 SessionComponent 的区别

标签: javascript php jquery session cakephp


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您确实需要,可以使用CakeSession(其中is used in Session component)。

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

    • prefix routing
    • 每个组/角色的不同观点
    • 具有菜单/按钮组的不同元素/...针对每个组/角色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 2020-12-16
      • 2013-09-10
      相关资源
      最近更新 更多