In this episode, we're getting ready to clean up our codebase by organizing some helper functions. Rather than scattering function definitions throughout the app (which can get messy fast), we're making these helpers globally available in a clean, maintainable way.
We start by looking at the usual approach—just declaring functions somewhere in our bootstrap code—but decide we want something tidier. So, we use Composer's ability to autoload files, setting it up in our composer.json
file right underneath the PSR-4 namespace registration.
We then create a new helpers.php
file in our app, register it in Composer, and add a sample name()
function. After dumping Composer's autoloader, these helpers become available anywhere in our application. To make sure everything works, we quickly call our new function from the Home controller and see the expected result.
In the next episodes, we'll get into adding genuinely useful helper functions that will help tidy up our code even more!