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

后端 未结 8 873
孤街浪徒
孤街浪徒 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:31

    Add this in app/start/global.php

    require app_path().'/config/autoload.php';
    require app_path().'/start/loader.php';
    App::instance('loader',new loader($autoload));
    

    create a new file loader.php in app/start and add:

    class loader{
    private $helpers = array();
    public $autoload = array(
        'helpers' => array()
    );
    function __construct($autoload  = array()) {
        if (!empty($autoload))
            $this->autoload = $autoload;
        foreach ($this->autoload as $key => $value)
        {
            $function = strtolower($key);
            $this->$function($value);
        }
    }
    function helpers($helpers=array())
    {
        if (!is_array($helpers))
            $helpers = explode(",",$helpers);
        foreach ($helpers as $key => $value) {
            $this->helper($value);
        }
    
    }
    function helper($helper = '',$path = '/')
    {
        $folder = app_path().'/helpers'.$path;
        if (file_exists($folder.$helper.'.php') && !in_array($helper, $this->helpers)){
            $this->helpers[] = $helper;
            require $folder.$helper.'.php';
        }
        else{
            $segments = explode('/',$helper);
            if (is_dir($folder.$segments[0])){
                array_shift($segments); 
                $this->helper($segments,$path.$segments[0].'/');
            }
        }
    }
    

    }

    create a new file autoload.php in app/config and add:

    $autoload['helpers'] = array('functions'); // my autoload helpers!
    

    create a new folder helpers in app/ , add your helper files. ( es. myhelper.php )

    function myhelper()
    {
    echo 'helper';
    }
    

    in your controller add:

    App::make('loader')->helper('myhelper');
        myhelper();
    

提交回复
热议问题