3

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

1
  • There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. Commented Apr 18, 2015 at 0:19

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

Comments

7

My problem was I used namespace in my helper.php file. Basically it has no class no namespace just some plain function you want to place there

Comments

0

You may need to use the class name in front of the function when calling it in your view or controller, e.g.:

Helper::my_function()

Comments

0

I found I had this problem too at some point, in which I had put my helpers under app/Helpers/functions.php instead of app/Http/helpers.php. Moving them to the latter made everything start working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.