dpzjl68484 2013-11-27 17:10
浏览 52
已采纳

PHP路由:在View中显示flash消息(Klein)

I am using Klein php routing for a simple app

the documentation is ok for using the library, however it is not good at how to implement the views

for instance I want to display a flash message on success/error/warning etc

i understand how klein can store the flash like this error message in documentation

$klein->respond(function ($request, $response, $service, $app) use ($klein) {
    // Handle exceptions => flash the message and redirect to the referrer
    $klein->onError(function ($klein, $err_msg) {
        $klein->service()->flash($err_msg);
        $klein->service()->back();
    });

so for my success message i did this

$service->flash("Success", $type = 'success' );

but other than foreach on the $_SESSION __flash, i cannot see how to implement this in my view

i surely think there is a render() or something i am missing...i mean otherwise why call all this when i can just store my own msg in a session, doesnt make much sense to me

anyways, any help is always appreciated

  • 写回答

1条回答 默认 最新

  • douzhuanqian8244 2014-01-17 20:13
    关注

    If you look at the docs for the latest version, there is a method Klein\ServiceProvider::flashes():

    http://chriso.github.io/klein.php/docs/classes/Klein.ServiceProvider.html#method_flashes

    The docs say it retrieves and clears all the flash messages, or all the flash messages of a given type.

    This is not to be confused with Klein\ServiceProvider::flash(), which adds a flash message:

    http://chriso.github.io/klein.php/docs/classes/Klein.ServiceProvider.html#method_flashes

    Here's the source:

    https://github.com/chriso/klein.php/blob/master/src/Klein/ServiceProvider.php#L179

    It looks like it returns an array of flashes, grouped by type, so you could foreach and echo them. If you're using the Klein templating system (in your case, you would render the template with $klein->service->render($myTemplateName)), then you can call the ServiceProvider from the template as $this.

    So in your template, you would have something like:

    <? foreach($this->flashes() as $type=>$messages): ?>
        <? foreach($messages as $msg): ?>
            <div class="alert alert-<?= $type ?>"><?= $msg ?></div>
        <? endforeach; ?>
    <? endforeach; ?>
    

    Obviously, you don't have to use the alternate control structure syntax, but I like to use it in my templates. It was part of the coding standard at a dev shop where I worked, and I adopted it as my own.

    Just a heads up. The docs seem to represent the code in dev-master, rather than the 2.0.x branch they tell you to use on the GitHub page. A lot of code seems to have been moved around since then (at least we know it's not abandoned, right?). I found the dev-master branch to be much less broken.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求