Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moraleida's avatar

Override functions in (...)/Support/helpers.php

I have a form full of nested values that are handled as arrays and I'm trying to validate it properly using Laravel's bult-in validation. I keep running into an Error Exception about htmlentities receiving an array instead of string on the e() function. As far as I can tell this happens when all the validation is done and the framework is trying to repopulate my form for the returned view with errors:

if (!function_exists('e')) {
    /**
     * Escape HTML entities in a string.
     *
     * @param  \Illuminate\Support\Htmlable|string  $value
     * @return string
     */
    function e($value)
    {
        if ($value instanceof Htmlable) {
            return $value->toHtml();
        }

        return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
    }
}

Where should I put my own e() function so that it will be read before this one?

(I've seen this: http://stackoverflow.com/a/28475973/1001109 but it seems quite hackish)

0 likes
7 replies
mul14's avatar
mul14
Best Answer
Level 7

You can do the simple trick. Just create new helper file, and include that file before composer autoloader in bootstrap/autoload.php.

Overwrite Laravel Helper

1 like
mul14's avatar

Sorry, I haven't read the Stackoverflow link before.

Alternatively, you can add namespace to the helper function.

<?php
namespace hello {

    function e() {
        echo "Helper with namespace";
    }

}

Now you can use both helpers

hello\e();        // new helper
e('Hi');          // Laravel default helper
moraleida's avatar

Thanks @mul14 I went temporarily down the route of changing bootstrap/autoload.php and it's working fine. But isn't that going to be wiped when I update laravel?

Your second solution is cleaner but won't help me. I need to redefine e() so that the Validator can use my function instead of the one defined in helpers.php. That's why namespacing is not an option, unfortunately.

mul14's avatar

It's ok. When you update the Laravel by doing composer update, the composer will replace/update vendor/laravel folder. The other folders will not affects.

funkjedi's avatar

First apologies for necroposting. But I ran into this problem recently and the bootstrap/autoload.php method doesn't work if you're using PHPUnit. I figured I'd share my solution for posterity sake.

I wrote a Composer Plugin which enables Composer to include project files prior to including vendor files. https://packagist.org/packages/funkjedi/composer-include-files

  1. Require the plugin composer require funkjedi/composer-include-files:dev-master.
  2. Update your project's composer.json file, adding your helper file to "include_files".
// composer.json (project)
{
    "extra": {
        "include_files": [
            "app/Foundation/helpers.php"
        ]
    }
}
9 likes
jhoff's avatar

@funkjedi Thanks for this. Very useful and much nicer than having to modify bootstrap/autoload.php

Could you please tag a release so we don't have to use dev-master? It'd be nice to not have to lower the minimum-stability.

For now, i'm just using "funkjedi/composer-include-files": "dev-master as 1.0" as a workaround, but a tag would be preferred. Thanks!

1 like

Please or to participate in this conversation.