27740

How can I write an accessible class in the whole of project?

Question:

I use Laravel framework and this is my current directory:

<a href="https://i.stack.imgur.com/7jDz6.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/7jDz6.png" data-original="https://i.stack.imgur.com/7jDz6.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

As you see, there is a class named Log <em>(the one I've selected)</em>. Now I need to make it global. I mean I want to make it accessible in everywhere and be able to I make a object <em>(instance)</em> of it in following files:

<ul><li>All files of classe folder</li> <li>All controller </li> <li>web.php file of </li> <li>All file of views</li> </ul>

Anyway I want to be able to make a instande of it and call its methods everywhere like this:

$obj = new Log(); $obj->insert($message);

How can I do that?

Answer1:

You can <a href="https://stackoverflow.com/questions/37339475/how-to-create-helper-methods-on-laravel-not-a-facade/37339565#37339565" rel="nofollow">create global Laravel helper</a>:

if (! function_exists('log')) { function log($message) { (new Log)->insert($message); } }

Put it in helpers.php and add this to composer.json to load the helpers file:

"autoload": { .... "files": [ "app/someFolder/helpers.php" ] },

Then you'll be able to use this helper globally:

log('User added');

In views:

{{ log('User added') }}

<strong>Update</strong>

@stack, you're using wrong syntax for JSON (screenshot in comments), here's correct one:

"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" }, "files": [ "app/Helpers/helpers.php" ] },

Recommend

  • C++ value representation of non-trivially-copyable types
  • Reshaping unusual data set [duplicate]
  • After update iOS 9 and Facebook sdk 4.6 the login window not open
  • Reference a .NETStandard 2.0 project from UWP project
  • Multiple htaccess rewrite rule
  • Package-specific logging levels for different Logback appenders
  • display all data in a table from sql in asp.net using C#
  • TypeError: 'int' object is not subscriptable?
  • Rails Returning ActiveRecord::Relation as json to an ajax call
  • python - regex why does `findall` find nothing, but `search` works? [duplicate]
  • Extract images with podofo from pdf pages
  • append to the same list with multiprocessing - python
  • Passing HTTP Post from AWS API GW to Lambda
  • Corda: How to implement hierarchical relationships between state data persisted to H2
  • Adding Dynamic Row and Data on Checkbox Click
  • Example of using Service Exists MSBuild task in Microsoft.Sdc.Tasks?
  • Disabling swipe gesture in Windows Phone 8.1 pivot control
  • Circular Left Rotation Algorithm in C#
  • Generate and export point cloud from Project Tango
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Create an average of multiple excel chart without the data source
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to split wav file into two or more parts using c#
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • JQuery: Infinite input select
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • how to run ejabberd with Erlang on Heroku?
  • How to call jQuery function in HTML returned by AJAX
  • How to use Streams api peek() function and make it work?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • How to warp text around image in iOS?
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Using Service Component Runtime
  • WPF custom control and direct content support
  • Running R's aov() mixed effects model from Python using rpy2
  • How to handle a codeigniter PDF generator
  • How to check if object is null in Java?