【问题标题】:Session flash won't accept new CSS classes in Phalcon PHPSession flash 不接受 Phalcon PHP 中的新 CSS 类
【发布时间】:2014-05-02 08:12:32
【问题描述】:

我正在处理一个简单的 Phalcon PHP 项目,我遇到了一个奇怪的问题,我想了解更多,但我似乎无法找到帮助我解决困惑的信息。

$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session([
        'error' => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info',
    ]);

    return $flash;
});

当我从控制器执行以下操作时,正在生成 <div class="successMessage">The data saved with no errors.</div>

if(empty($errors)){
        $this->view->disable();
        $this->flash->success('The data saved with no errors.');
        $this->response->redirect('dashboard/');
    }

我希望<div class="alert alert-success">The data saved with no errors.</div> 所以目前我假设我做错了什么。我的会话似乎很好,所以我没有探索过。

环顾四周,我发现以下代码直接放在$di->set('flash') 之后。当我尝试它时它确实有效,但它从我的配置文件中生成 html,这不是 MVC,并且也意味着 html 是在我的容器 div 之外生成的,这使它真的是 wiiiidddee。

if (!count($_GET))
{
    $di->get('flash')->error('Error');
    header("Location: ?t=1");
}else{
    $di->get('flash')->output();
}

见:https://github.com/phalcon/cphalcon/issues/2136

所以如果有人知道发生了什么,请赐教。 Phalcon 的人在其他论坛上坚持认为这没有错误。

【问题讨论】:

    标签: php html phalcon


    【解决方案1】:

    嗯。我猜你做错了什么:)

    首先,默认 DI 已经有flashSession 服务,尝试使用它而不是flash,即不要用flash 覆盖它(如果您使用的是出厂默认设置)。

    二、documentation says

    “flashSession”属性是之前设置 flash 的方式 依赖注入容器。您需要开始会话 首先要成功使用 flashSession messenger。

    所以在你做任何事情之前确保你的会话已经开始,这实际上似乎是最可能的原因here你可以看到使用默认消息,除非你传递样式数组。这意味着,当您打印时,您的 flash 服务会在没有该数组的情况下启动……

    我只是在本地尝试过,效果很好:

    // In my di (not factory default di, just di)…
    $di->set('flash', function() {
        $flash = new \Phalcon\Flash\Session([
            'error' => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ]);
    
        return $flash;
    });
    
    // In my index controller before redirection…
    $this->flash->success('The data saved with no errors.');
    $this->response->redirect('somewhere');
    $this->view->disable();
    
    // In somewhere redirected controller…
    $this->flash->output();
    die;
    
    // In chrome output…
    <div class="alert alert-success">The data saved with no errors.</div>
    

    【讨论】:

    • 根据您的建议,我所做的是将$this-&gt;flash-&gt;output(); 添加到我的 BaseController 初始化函数中,现在一切正常。我在我的 volt 文件中通过 {{ content() }} 获得了错误的 HTML。也许功能请求是为了让 Phalcon 这样做,所以不需要执行上述操作?感谢你的回答;陡峭的学习曲线等等:)
    • 嗯……不过,整个想法是让这个工作在一个视图中。我需要调整很多东西来测试它,所以我没有打扰。您是否不需要在某些时候自定义输出在 html 中的位置?如果你发布更多关于如何从控制器获取到视图以及如何在视图中打印的详细信息,我可能会提出更多想法。
    • $this-&gt;flash-&gt;output(); 的问题它不返回任何值,它只是 echo 内部的东西。除非……您使用$this-&gt;flash-&gt;setImplicitFlush(false);,在这种情况下它将返回html 消息内容,然后您可以将其传递给您的视图。反正有点脏:)
    • 现在它就是我想要的地方。我从来没有见过一种方法来定制闪光信息和它的伏特位置。据我所知,视图中只有&lt;p&gt;&lt;?php $this-&gt;flashSession-&gt;output() ?&gt;&lt;/p&gt;
    • 它仍然来自 {{ content }} 因为它在我的
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多