2

I use Laravel framework and this is my current directory:

enter image description here

As you see, there is a class named Log (the one I've selected). 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 (instance) of it in following files:

  • All files of classe folder
  • All controller
  • web.php file of
  • All file of views

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?

stack
  • 10,280
  • 19
  • 65
  • 117

1 Answers1

2

You can create global Laravel helper:

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') }}

Update

@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"
    ]
},
Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 2
    Seems correct, thank you, upvote .. just for my information, can't I do that by making a *Facades* ? – stack Dec 13 '16 at 11:28
  • 1
    @stack Facades are for static methods, if you want an instance, a facade doesn't suit with your case. – Gerard Reches Dec 13 '16 at 11:30
  • You can, but using global helpers is much more robust and easier to use solution for this case. – Alexey Mezenin Dec 13 '16 at 11:30
  • Can you please tell me what's the difference between your solution and the other one in the link you provided ? Both of you have used helper, right? so what's the different? – stack Dec 13 '16 at 12:23
  • Both solutions are exactly the same. I just adapted code in my answer for you situation. – Alexey Mezenin Dec 13 '16 at 12:27
  • @AlexeyMezenin No no, I know your two solutions are the same, my question was about your solution and [this one](http://stackoverflow.com/questions/37339475/how-to-create-helper-methods-on-laravel-not-a-facade/37339565#37339615) – stack Dec 13 '16 at 12:29
  • 1
    Oh, sorry, I get it now. My solution uses composer to load file. Another solution manually loads bunch of files. – Alexey Mezenin Dec 13 '16 at 12:31
  • Do you know what are those two red underlines? has my code syntax error? https://i.stack.imgur.com/OIO99.png – stack Dec 13 '16 at 12:40
  • Yes, you're using wrong syntax for JSON file. I've updated my answer with an example. Hope this helps. ) – Alexey Mezenin Dec 13 '16 at 12:46
  • Sorry for asking too much, it's the last one *(I promise)*, Why did you use `files` word in the composer? Can it be anything else or it is a constant? Also for doing what job do I need to use `files`? – stack Dec 13 '16 at 12:58