Public Available Function in Laravel 4

2020-07-22 03:33发布

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?

2条回答
劳资没心,怎么记你
2楼-- · 2020-07-22 04:16

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');
查看更多
贼婆χ
3楼-- · 2020-07-22 04:17

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';
查看更多
登录 后发表回答