Laravel : Calling functions defined in base_controller from view

Jim picture Jim · Nov 20, 2012 · Viewed 15.3k times · Source

In using the laravel framework, how can I call a function defined in base_controller, in a view. For exacmple:

class Base_Controller extends Controller {

    public static function format_something()
    {
         return something;
    }
}

How can i call format_something() in a view file?

Usually the error I get looks something like this: Method [link_to_action] is not defined on the View class.

Probably a silly question, but thanks in advance!

Edit

Okay! First the correct place to do something like this is in the libraries folder. Second, problem is that your class cannot have underscores.

So in application/libraries I made file AppHelper.php with class

class AppHelper {

    public static function format_something()
    {
        return something;
    }
}

And can call it like:

$formated = AppHelper::format_something;

Thanks for the help and the good forum find Boofus McGoofus.

Answer

Lajdák Marek picture Lajdák Marek · Jun 2, 2013

For me is working:

Create directory "helpers" or whatever and file:

// app/helpers/AppHelper.php

class AppHelper {

    public static function format_something()
    {
        return something;
    }
}

Add path to composer.json

// composer.json

    "autoload": {
        "classmap": [
                    "app/helpers"   // <-------- add this line
        ]
    },

Run: (reload the autoload)

composer dump-autoload

Now you can call:

$formated = AppHelper::format_something();