ChatGPT解决这个技术问题 Extra ChatGPT

Best Practices for Laravel 4 Helpers and Basic Functions?

I'm trying to understand the best place to put a global function in Laravel 4. For example, date formatting. I don't think making a facade is worth it as facades are too modular. I've read articles about creating a library folder and storing classes there but that also seems like a lot for a simple function. Shouldn't a 'tool' like this be available in Blade templates?

What are the best practices for something like this and how do I make it available to Blade templates?


A
Alexandre Danault

The ugly, lazy and awful way: At the end of bootstrap/start.php , add an include('tools.php') and place your function in that new file.

The clean way: Create a library. That way it'll be autoloaded ONLY when you actually use it.

Create a libraries folder inside your app folder

Create your library file, create a class in it, and add static functions to it

Option 1: Edit start/global.php to add app_path().'/libraries' to the ClassLoader::addDirectories( array.

Option 2: Edit composer.json to add "app/libraries" to the autoload array. Run composer dump-autoload

Call your class and static functions from your views.

About your options, quoted from the global.php file

In addition to using Composer, you may use the Laravel class loader to load your controllers and models. This is useful for keeping all of your classes in the "global" namespace without Composer updating.

You can combine both options, where the Laravel class loader will automatically search for classes in the registered directories (Option 1, easier) and Composer will keep record of all the classes but only after you update it (Option 2, might improve performance).


I think you also have to place app_path().'/library', in start/global.php.
@AlexandreButynski Good point. This will allow to add libaries without having to update composer everytime. I edited my post to add this.
So i create a class called IDK... SiteHelpers and then define a method formatDate. and then in the template I should be able to do SiteHelpers::formatDate($date) ?
@JasonSpick Yes, and make sure you name your file sitehelpers.php
Why do you need to add it to composer's autoload as well as the global.php file?
J
JasonMortonNZ

My way of doing this is to create a new folder in the /app directory in the root of your Laravel 4 project. Then add this folder to the first array of the /app/start/global.php file like so:

<?php

ClassLoader::addDirectories(array(

app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', // This line is the one I've added.

));

As long as the folder structure within the new /app/classes folder follows your namespacing convention. Laravel 4 will autoload all the classes/files within this folder. This way there's no need to dig into any composer files or run composer command.

Not sure if this is best practice but it certainly works.

If you created a simple file called /app/classes/Helpers/Helper.php such as this:

<?php namespace Helpers;

class Helper {

    public static function helloWorld()
    {
        return 'Hello World';
    }
}

All you would need to do is call Helpers\Helper::helloWorld();

You could also alias this helper class in your /app/config/app.php file. Just add something like this to the end of the aliases array:

'Helper'          => 'Helpers\Helper'

I like this way because you dont have to mingle with anything composer-ish, a clean and classy solution. +1
If you were to add your helper class to your app.php file you can now access the class directly like this: Helper::hellowWorld();
should the helper be a class? can I define a simple function here?
This doesn't work if you don't edit the composer.json and don't run composer dumpautoload
Great solution. As Rommy says, don't forget to run composer dump-autoload - I did and spent 20 mins scratching my head.
i
iconoclast

Laravel's helpers.php method is to add it to your "files" in composer.json (https://github.com/laravel/framework/blob/master/composer.json):

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

What I do is to create small classes (a few methods per class, one line per method, everything extended from something and DRY, that's my goal),

class ExtendedCarbon extends Carbon\Carbon {

    public function formatDDMMAAAA($date)
    {
        /// format and return
    }

}

save them to them in app/libraries and add to composer.json:

"autoload": {
    "classmap": [
        ...
        "app/libraries",
        ...
    ],
},

Execute

composer dump

And then just use them wherever you need

$formatted = (new ExtendedCarbon)->formatDDMMAAAA($date);

Watch this video about refactoring: http://www.youtube.com/watch?v=DC-pQPq0acs

By the way, I'm kind of sure it was just an example, but you might not need a helper to format dates, since all dates in Laravel are instances of Carbon (https://github.com/briannesbitt/Carbon) and it has loads of methods to format date and time.


I'm not such a big fan of static methods myself, and try to use instance methods when necessary. Bug I'm pretty sure that in the case of such usage, you would be much better of having formatDDMMAAAA() as static.
F
Franz

You can also use View::share() together with closures to achieve this - I just posted about this: http://www.develophp.org/2014/07/laravel-4-blade-helper-functions/

Added benefit: You don't need to create an extra class and also keep the global namespace clean.


I like this approach. More rail-ish. Could we some how add all the view-helper methods inside a trait and have them available in the blade view?
I don't think that's possible with PHP, to be honest.