Creating Your Own PHP Helpers in a Laravel App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Today, let's talk about helpers. Laravel provides many. Everything from array helpers to string helpers to even little utility functions like the dd or the dump and die function. All those are just little helper functions. So there comes a time in a lot of apps where you might need your own. And to give you an example, for the Laravel news website, we have a single helper and it's used for generating image URLs. It uses a config item and if it's turned on, then we pull images from a CDN URL. If it's turned off, then we pull them from the file system. And the reason we grouped that into a little helper is because it's used throughout the app and it was just sort of an easy way to set all this up. So if you ever want to create helpers in your own app, let's look at how you can do that and how to set up your composer file to pull in the helpers and even how to structure them and all that. So let's dive into the code and take a look. So here I have a brand new Laravel 11 app and the first step is where do I put the helpers file? What I like to do, and you can do it anywhere you want, it's pretty free so you can use your imagination, but I just throw it in the app folder. And we're going to create helpers dot PHP. And now let's just create a simple, I don't know, image helper. We're going to pass in a URL and just return that URL. Something super simple. And now, you know, we might want to call this. So let's open up the routes web and we'll just dump it out right here. So we're going to say image. And there you go placeholder something or other. So let's save that. Now if we open it in the browser, you can see we instantly get an error. And that's because the function is not defined. And, you know, the app, the PHP itself doesn't know where to find that function. So we have to actually register it. All we have to do is open our composer dot JSON file, scroll down to the auto load. And right here we can just define files and app helpers dot PHP. Put a comma there, save that. Now let's open up the terminal and we're going to go composer dump. Close that back out. Open the browser, refresh. And you can see right there we have this item dumped out. Just as we would expect. So one tip is in your helpers file, what you should do is wrap every function with an if statement to make sure that function hasn't been defined elsewhere. You know, maybe in one of the core files, maybe in a package you're using, anything like that. And that's just sort of, you know, a safe way of making sure it's not already there. And you can see we're basically just wrapping it in this if not function exists. And then we run the function. So pretty simple. But that is a simple way to register helpers for your own apps. And a little tip there for making sure it's not going to be a conflict with something else. And one final tip, speaking of naming collisions, it's probably not a bad idea to prefix your own functions. That way you know it's from your app itself. And in theory, that should never conflict. For instance, here we're using the name of image. Because this is for the news website, I might just prefix everything with LN. And that's just kind of one little safety precaution, I guess you could say. But there's how to register helpers in your own app. I hope it helps and go and make awesome stuff. Be sure, like and subscribe and leave us a comment. You know, let us know what to cover next. And if you have any tips that we missed on this, let us know.
Info
Channel: Laravel News
Views: 3,563
Rating: undefined out of 5
Keywords: laravel, php, laravel tutorial, laravel tutorial for beginners, laravel helper, how to register helper in composer, how to create custom helper, how to create helper function, configuration of customer helper in laravel, create custom helper
Id: ob8is94hHJE
Channel Id: undefined
Length: 4min 22sec (262 seconds)
Published: Tue Mar 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.