Php – helper functions not loading laravel 5 – Call to undefined function

composer-phplaravellaravel-5PHP

I've been having a problem loading my helpers file in Laravel 5 and calling the functions from views. The below is a quick overview of how I attempted to setup helpers within my L5 Project.

  1. Created a helpers.php file within app/Http;
  2. The helpers file contains a basic function, to produce the page title.

    public function full_title($title)
    {
         $base_title = 'Social Tracking Application';
    
         if (isset($title) && $title != ''){
            $comp_title = $title . " | " .  $base_title;    
         } else {
           $comp_title = $base_title;
         }
    
    return $comp_title;
    }
    
  3. Updated composer.json to autoload the new helpers file.

    "autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files":["app/Http/helpers.php"]
    

    },

  4. Run composer dump-autoload from the terminal.

After these steps I was under the impression that I could use my helper from views and controllers. So I placed a call to my new helper function within my main layout view by using the following:

<title> full_title($title)  </title>

However when I navigate to any page I receive a FatalErrorMessage Call to Undefined Function full_title. (note the message is usually in the title bar so I move it to a body tag to view the error message).

I can't figure out what it is I'm doing wrong here, I've seen other examples of people using custom helper files in a similar manner but mine just doesn't seem to work am I missing something obvious?

I'm running this of Homestead.

full code base: https://github.com/n1k41l/SocialTrak/tree/middleware-currentUser

Best Answer

I've managed to solve the problem myself, the issue was that I created my Helpers file as a php class. The helpers file should only be a php file - with functions not a class with functions... (obvious mistake once I found it :S).

Related Topic