How to Call a static Method inside a blade in Laravel 5

Published 2 years ago by drake24

I have problem. Usually in Laravel 4 I have views / blades that will call static functions from my Helper folder which is located at app/helpers/helper.php. class Helpers{

public static function myCustomHelper(){
    return
        "Im a custom helper";
}

I can call it in my blade simple by {{ Helpers::myCustomHelper() }}. However, in Laravel 5 I get an error stating Helpers class is not found.

Best Answer (As Selected By drake24)
drake24

@nolros I think I got the answer.

I just added my App\Libraries\Helpers.php in config/app.php under class aliases

'Helpers'   => 'App\Libraries\Helpers'

Then it worked in blade {{ Helpers::myCustomHelper }}

michaeldyrynda

You'll need to reference the full namespace of Helpers in your template. If it's in the global namespace, you should be able to just prefix it with a backslash: \Helpers:myCustomHelper().

KbirMhrjn

or just create a helper function so that it can be referenced from anywhere.

function example(){
return App\MyClass::myMethod();
}

and call it from the view, just like

{{example()}}
nolros
nolros
2 years ago (63,940 XP)

@drake24 most likely you had it autloaded in your composer.json file. You can go back and do that if you want:

"autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],
        "psr-4" : {
            // whatever the path is for your hlper
            "App\\":"app/Helpers/myCustomHelper"
        }
    },
drake24

@nolros Tried it but get this error Uncaught exception 'ReflectionException' with message 'Class App\Http\Kernel does not exist' in C:\xampp\htdocs\laravel\storage\framework\compiled.php on line 1026

drake24

I've tried placing it inside my blade

and calling it {{ $Helpers::myCustomHelper() }}

It works. However, I find it dirty code to have it on top of my blade files.

nolros
nolros
2 years ago (63,940 XP)

did you run composer dumpautoload after placing it the composer.json file? Errror message suggest you might not have.

drake24

Yes, I did run dump-autoload

"autoload": { "classmap": [ "database", "app/libraries" ], "psr-4": { "App\": "app/", "App\": "app/libraries/helpers" }

nolros
nolros
2 years ago (63,940 XP)

You missing double slash after App. Also unsure if you need the path in classmap. Try it like this then dumpautoload.

"autoload": { 
  "classmap": [ 
    "database"
  ], 
  "psr-4": { 
    "App\\": "app/", 
    "App\\": "app/libraries/helpers" 
  }
drake24

Same result @nolros

Fatal error: Uncaught exception 'ReflectionException' with message 'Class App\Http\Kernel does not exist' in C:\xampp\htdocs\laravel\storage\framework\compiled.php on line 1026

nolros
nolros
2 years ago (63,940 XP)

@drake24 really weird, try one thing if you have not already. Stupid question but the helper is in that directory right?

It is saying it cannot find the class in your compiled file so try regen.

php artisan clear-compiled 

if that does not work try:

php artisan optimize 
drake24

Yes it is really weird. Still the same error. Any route I add in the psr-4 still returns the same error

michaeldyrynda

Can you declare the same namespace twice? The second reference to App\\ will be overriding the first one, thus breaking references to any of Laravel's default classes.

drake24

So you're saying? that I'll use another reference? aside from "App\" @deringer

drake24

@nolros I think I got the answer.

I just added my App\Libraries\Helpers.php in config/app.php under class aliases

'Helpers'   => 'App\Libraries\Helpers'

Then it worked in blade {{ Helpers::myCustomHelper }}

michaeldyrynda

@drake24 what you were doing was overriding the default application namespace. If you say "everything for the namespace App is in the folder app/libraries/helpers then everything Laravel is trying to load will be missing.

Sign In or create a forum account to participate in this discussion.