我在哪里可以显示Flash消息的Laravel 4帮助器函数放在哪里? [英] Where do I put Laravel 4 helper functions that can display flash messages?

查看:66
本文介绍了我在哪里可以显示Flash消息的Laravel 4帮助器函数放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的display_messages()函数,该函数将在Session::get('errors')中搜索闪存数据并将其回显到屏幕上.

I've written a simple display_messages() function that will search Session::get('errors') for flash data and echo it to the screen.

该功能放在哪里?在Codeigniter中,您有一个helpers文件夹,您可以在其中粘贴所有小的全局助手方法.

Where do I put this function? In Codeigniter, you had a helpers folder where you could stick all your little global helper methods.

推荐答案

按照乌斯曼的建议,

  • 创建文件/application/libraries/demo.php
  • 在其中定义一个class Demo() {
  • 这样调用函数:{{ Demo::display() }}
  • create a file /application/libraries/demo.php
  • define a class Demo() { inside it
  • call the function like so: {{ Demo::display() }}

有效,因为库和模型是在start.php第76行中自动加载的.我相信文件名必须与类名匹配(注意大写).

Works because libraries and models are autoloaded in start.php line 76. I believe that filenames must match Classnames (note capital).

<?php

class Demo {

    public static function display() {

        if( !$message = Session::get('errors'))
            $message = 'No Errors';

        echo "<pre>print_r($message)</pre>";

    }

}

无法完全弄清楚为什么我在使用通用类名时会遇到问题,可能会发生冲突(如果这很重要,则可以定义一个名称空间)...

Can't quite figure out why I had a problem using the classname Common, there may be a conflict (you could define a namespace if this were important)...

这篇关于我在哪里可以显示Flash消息的Laravel 4帮助器函数放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆