Laravel offers a wide range of useful helper functions that simplify tasks like manipulating arrays, file paths, strings, and routes, including the popular dd()
function.
You can create custom helper functions for your Laravel projects and PHP packages by leveraging Composer for automatic imports.
If you're new to Laravel or PHP, here's a walkthrough on creating and loading your own helper functions seamlessly within Laravel.
Table of contents:
One common practice is to place helper functions within a Laravel application. You have the flexibility to organize helper files in various locations, such as:
In my case, I usually keep them in app/helpers.php within the primary application namespace.
To utilize your PHP helper functions effectively, they need to be loaded dynamically at runtime. Instead of manual inclusion using require
or require_once
, Composer provides a more efficient solution for autoloading.
When setting up a new Laravel project, the composer.json file contains autoload and autoload-dev keys. You can specify your helpers file under the files key within autoload, enabling automatic loading:
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
}
Remember to run composer dump-autoload
after modifying the autoload configuration to ensure the autoloader picks up the changes.
When defining functions in your helpers class, ensure to prevent function definition conflicts by wrapping them in function_exists
checks. This guards against unexpected clashes in function definitions due to the order of inclusion.
By adhering to naming conventions and potentially prefixing function names, you can reduce the likelihood of collisions and enhance the clarity of your codebase.
For example, in Monse, one of my clients, we have some helpers to format numbers and they are defined like this:
if (! function_exists('format_currency') {
function format_currency(int $amount, string $currency): string
{
$locale = match ($currency) {
'EUR' => 'es_ES',
// ...
default => 'en_US',
};
return (new NumberFormatter($locale, NumberFormatter::CURRENCY))
->formatCurrency($amount / 100, $currency);
}
}
Then you can directly use this function everywhere in your project because it's already preloaded with composer.
When developing Composer packages, consider including a helpers file to provide essential functions to projects using your package. Make sure to use function_exists()
checks to prevent conflicts and choose unique function names to prevent clashes.
Check Composer's autoloading documentation for more insights on file inclusion and autoloading classes effectively.
Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.
Get 1 month free with yearly plan during checkout.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.