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

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

推荐答案

As Usman suggested,

  • 创建文件/application/libraries/demo.php
  • 在里面定义一个class Demo() {
  • call the function like so: {{ Demo::display() }}

之所以有效,是因为库和模型在开始时是自动加载的.php第76行.我认为文件名必须匹配类名(注意大写).

<?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)...

Laravel相关问答推荐

Uncaught ReferenceError:未定义require[Shopify PHP React Browser问题]

为什么使用 created_at 表在迁移错误中创建表?

当使用Laravel eloquent查询100000条数据时速度很慢

Laravel Eloquent - 随叫随到的加密/解密数据

Laravel 4:如何将 WHERE 条件应用于 Eloquent 类的所有查询?

覆盖 HTTP 标头的默认设置 (X-FRAME-OPTIONS)

Mockery 和 Laravel 构造函数注入

Laravel Auth::attempt() 返回 false

如何使用 ajax 请求删除 laravel 5.3 中的记录?

找不到列:1054字段列表中的未知列0-Laravel-我的代码中的任何地方都没有0列

如何创建具有关系的 Eloquent 模型?

Laravel 动作未定义

在共享主机中仅使用 FTP 部署 Laravel 5

如何从 Laravel 中的资源中获取图像?

assets资源不引用公用文件夹(Laravel)

在 Laravel 中结合 AND/OR Eloquent的查询

Laravel 删除集合中的第一项

如何在 Laravel 中显示动态复选框的旧数据?

如何判断 Laravel 集合是否为空?

以编程方式而不是从 CLI 运行 Laravel 5 seeder