Reputation: 445
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.
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;
}
Updated composer.json to autoload the new helpers file.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files":["app/Http/helpers.php"]
},
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
Upvotes: 3
Views: 14179
Reputation: 1655
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
Upvotes: 7
Reputation: 3223
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.
Upvotes: 0
Reputation: 2676
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()
Upvotes: 0
Reputation: 445
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).
Upvotes: 7