Laravel 4.x Hi I'm learning about Helpers. i have a Helper (helpers.php) that I want to load so my function in the View will be able to process the data.
My composer.json
...
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"files" : [
"app/helpers.php"
]
},
...
but by running : composer dump-autoload --optimize
I have no error but my view doesn't process the data
View
<img src="foo($photo->user->name) }}.jpg" alt="" >
My helpers.php
function foo($email)
{
//process some data here that will return the name
return "john";
}
If you don't get any errors about a non-existant function, I think your might be just wrong.
You have this:
<img src="foo($photo->user->name) }}.jpg" alt="" >
But should have this:
<img src="{{ foo($photo->user->name) }}.jpg" alt="" >
Create a classes
directory in your app/
Modify app/start/global.php
as
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', //add this
));
Create your Helpers.php
file in new created classes
directory.
<?php
class Helpers {
public static function myCoolFunction() {
return "so cool";
}
}
Sometimes you may need to run composer dump-autoload
.
Here you go: Helpers::myCoolFunction();
Also another quick way (there several ways actually), but not recommend, put your function to boostrap/start.php
. You can call wherever you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.