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

crazymonster's avatar

Laravel 10: Override Illuminate/Foundation/Helpers.php method trans()

Hi folks, I need to override trans() method in helpers in-built file. I tried to load my custom file under app/helpers.php and then load it in composer.json, I also tested creating a Service Provider, and loading my file in bootstrap/app.php but in all cases my custom file loaded after in-built helpers.php.

I found this: "for first Laravel check is this function exists, if it exists, Laravel will not define this function again(otherwise it will throw fatal error). So if you will define your function before autoloader include vendor/laravel/framework/src/Illuminate/Foundation/helpers.php file, you can define your custom response function.

Unfortunately there is no way to say composer load first your autoload.files section, then laravel autoload.files. But you can do small hack ...

open bootstrap/autoload.php file and include your file before autoloader

// file with your custom helper functions require DIR.'/../app/app/Http/helpers.php'; require DIR.'/../vendor/autoload.php';"

But this solution is for Laravel 5 and in Laravel 10 there is no bootstrap/autoload.php file. So, how can I override this helpers method in Laravel 10?

0 likes
1 reply
vincent15000's avatar

Why don't you simply create your own helper class with your own trans() function ?

class MyHelpers
{
	public static function trans($message)
	{
		// your code here
	}
}

You can add this class inside a Helpers folder inside the app folder.

And you can easily use your helper.

$translatedMessage = MyHelpers::trans($message);

Please or to participate in this conversation.