Startup Helpers in laravel 4

I am trying to get Laravel 4 to automatically download auxiliary files from the app/helpers (I created this, obviously).

I started from this Composer path: add the path to composer.json , and then run dump-autoload . This did not work. Then I tried with the app/start/global.php , which didn't work either.

Notice, I do not throw classes into auxiliary files - what are Facades and Packages for. I only need small helper functions like those that belong to Laravel (in the vendor directory). I just say this because it seems that the drafters of the dump list the classes (only with namespaces).

What can I do to get helpers to boot automatically?

Update

It also seems that the ClassLoader::addDirectories() function does not work for classes - why is it there? Should I use both this and Composer?

Edit

It seems my question is not understood. In my app/helpers , I have a file called paths.php . I want to be able to call a function inside it ( theme_path($location) ) in a global scope.

+4
source share
4 answers

Autoload is only for classes, you cannot automatically upload arbitrary php files based on one of your function names.

It is best to go to OOP and use classes for your helpers. You will only get the efficiency of auto-loading files when needed, and you'll be closer to what the rest of the Laravel community does.

But if you still want to use simple old non-objects php functions, I think you could just individually require() all of them from start.php, but they would all be loaded with every page request.

+3
source

You need to do the following:

 "autoload": { "files": [ "file location go here" ] }, 

Then you can use the functions in the auxiliary file. However, it is worth noting that this will load the file explicitly with every request. See http://getcomposer.org/doc/04-schema.md#files for more details.

I would recommend that if this is a heavily used helpers file, don't do this and just organize it with namespaces and classes to avoid name collisions.

+9
source

Ok ... I can show you, my composer and assistants, to show how I made it work:

 "autoload": { "psr-0": { "Helpers": "app/libraries" } } 

And then in app / libraries / Helpers / DateHelper.php (for example)

 <?php namespace Helpers; class DateHelper extends \DateTime { } 

After creating the auxiliary file, I just ran

composer dump-autoload

And now just use your helper as Helpers \ DateHelper

0
source

Not directly relevant to your question, but Laravel integrated the Carbon library ( https://github.com/briannesbitt/Carbon ) to handle dates. It is located inside the vendor/nesbot , so you can use it directly in Laravel.

0
source

Source: https://habr.com/ru/post/1487502/


All Articles