php - Laravel where to put code that can be accessed from anywhere? -


i have following in controller, gets user's avatar s3, , if have avatar return s3 url otherwise return default.

public function getavatar(request $request){     $userid = auth::user() ? auth::user()->id : 0;     $url = url('/cdn/avatar/default.png');     if($userid > 0){         $md5    = md5($userid);         $disk   = storage::disk('s3');         $exists = $disk->has('avatars/' . $md5 . '.png');         if($exists){             $url = env('url_avatars') . $md5 . '.png';         }     }     return response()->json($url); } 

i don't want have write every time, want move centralized location can call anywhere. still new laravel, not sure put code.

do make service provider? middleware? else? how access new method?

just create helper file in apps folder.

<?php //helper.php  function makeavatar() {   // avatar function   // return avatar } 

now can call function anywhere in application long auto-load file.

to that, go composer.json file , add following

"autoload": {         "classmap": [             "database",         ],         "files":[             "app/helper.php"  // add section         ],         "psr-4": {             "app\\": "app/",         }     }, 

just focus on files section.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -