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

后端 未结 8 878
孤街浪徒
孤街浪徒 2021-02-12 14:07

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

相关标签:
8条回答
  • 2021-02-12 14:43

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

    0 讨论(0)
  • 2021-02-12 14:45

    Create a folder helpers within your app folder and create a file application_helper.php. With such code:

    // app/helpers/application_helper.php
    
    function display_messages()
    {
      exit('Yes');
    }
    

    Then open your composer.json file in root. autoload app/helpers/application_helper.php with composer files.

    "autoload": {
    ....
    
        "files": [
            "app/helpers/application_helper.php"
        ]
    

    Done, you can now call display_messages().

    Some autoloaders may require you to run composer dump command for the first time.

    0 讨论(0)
提交回复
热议问题