Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I put Laravel 4 helper functions that can display flash messages?

Tags:

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.

like image 350
Chris G. Avatar asked Feb 15 '13 20:02

Chris G.


People also ask

What is flash message in Laravel?

A flash message is used to communicate back to the user of the website or application that an event has taken place. Often times you'll see a redirect with flash message. This may be something the user intended to do, or it might be something that is just informational.

What are helpers in Laravel?

Basically, helpers in Laravel are built-in utility functions that you can call from anywhere within your application. If the helper you need isn't provided by the core framework, you can code your own helper classes. You'll learn how to create a custom Laravel helper in this tutorial.


1 Answers

As Usman suggested,

  • create a file /application/libraries/demo.php
  • define a class Demo() { inside it
  • call the function like so: {{ Demo::display() }}

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

like image 167
ptim Avatar answered Sep 17 '22 01:09

ptim