If I would like to make a function in Laravel, that is available everywhere in my app, how would I go about that?
What I mean, is that, if I create a function called resizeimage() and would like to call it from anywhere in my Laravel application, how can I do this?
Create a folder named: libraries
Create a file(class) in your library: Image.php
Then add this code to Image.php
:
<?php
class Image{
public static function resizeImage($image){
return $image;
}
}
Edit global.php
in your 'start'-folder: Add app_path().'/libraries',
Example:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries',
));
Now you can call your 'function' like this 'anywhere':
Image::resizeImage('http://path/to/image');
You could create a helpers.php file inside app and require it on app/start/global.php.
app/helpers.php
<?php
function resizeimage() {
// resize image
}
app/start/global.php
// ...
// At the bottom
require app_path().'/helpers.php';