DEV Community

Cover image for Build your First Laravel Helpers
Mina Farag
Mina Farag

Posted on

Build your First Laravel Helpers

What are Helpers?

helpers are all the little tiny magical methods you'll create today. It's a more advanced way of increasing your code efficiency and clearance.
In a WebDev way of declaring stuff: It's group of custom-built methods but not classes.


Let's Set it up.

[1]
using any IDE - i use sublime myself. Please create a new (.php) file & name it (helpers.php), Then save it to your app directory.

your-side (dir) will be like

app/helpers.php

note

  • we don't use the (.blade.php) layer for this file.




[2]
let's assume you wanna inject your layout.blade.php template file with FontAwesome Icons-Api. So the usual way is to just add a link tag referencing your Api like the following:

code

<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"crossorigin="anonymous">
</head>

note

  • i've deleted part of the link ,but you can have a new full-api-link here

[2-2]
Side-Question/ Please think with me, What if you wanna use more apis?
Answer/ aooch A head tag filled with blocks of multiple unclean lines.

the Beautiful Laravel





[3]
give yourself [5-minutes] : eyes-looking-to-the-ceiling. Work your mind to create a feature that can clean up our code? you can use a paper and a pencil.

hmmm

[3-2]
now We're talking about a helper feature or functionality & Yeaaa for sure this is one tiny purpose of using Laravel Custom Built Helpers. So let's get back to our empty file ( app/helpers.php ) and start adding some code to it as shown:

code

<?php
function fontawesomeloader(){
echo '<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"crossorigin="anonymous">';
}

note

  • i prefer using snake-case naming style for Laravel helpers.

[3-3]
now let's get back to our layout.blade.php please Erase the previous link tag was added in [Point: 2] , Then please replace it with the helpers method-name we added in [Point: 3-2] fontawesomeloader(). Now Refresh your page. And Ohhh nothing works what's wrong ?!!

  • laravel will throw an exception saying it can't find any function called fontawesomeloader(). let's fix that.
  • remember we declare the helpers function name like this {!! fontawesomeloader() !!}

[3-4]
please goto a file within your main project directory called (composer.json) , you'll find it in a directory like this (LaravelProjectName/composer.json).

Within file please search for "autoload": {

[3-5]
now edit the code inside the "autoload" object to be like the following code

code

"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files":[
"app/helpers.php"
]
}

note

  • we added our helper.php file location inside the files-Array ,This is called __ Registering __ your Helpers.

Now you've registered your helpers.php file and open it for random call/access within anyfile inside your project directory.

and Oaoooo , nothing works AGAIN.

[3-6]
One final step to turn it on forever. Please go to your Terminal. and cd to your project directory then execute the following command

code

composer dump-autoload




[4]
Ohhhh FINALLY NOW IT's WORKING.
Great News,your helpers are up and running & you can easily add as many methods as you like.

You've done a great job learning and setting up your first Laravel Helpers.
and am very happy and proud of you.

[4-1]

Isn't Laravel so beautiful ?



[5]

Thanks for your time and patience.

i hope i was useful to you. please try it out and modify the code i wrote. Develop your own helpers and comment it out here if you like.

Oldest comments (6)

Collapse
 
gabe profile image
Gabe

Laravel looks so nice. I’ve been dying to try it out, was considering switching my current cms from grav to octobercms just so I can give laravel a real shot!

Any recommended readings for those new to laravel and php in general?

Collapse
 
__mfarag profile image
Mina Farag

You know, you will never regret trying laravel. It’s a powerful , clean and beautiful platform.

You can find useful laravel resources in :

  • Laravel.com /documentaries
  • Laracasts.com
Collapse
 
gabe profile image
Gabe

Gave OctoberCMS a try tonight and got a chance to use laravel. I’m hooked, going to build out a custom template and use that as a new blog. Thank you for this!

Thread Thread
 
__mfarag profile image
Mina Farag

OctoberCMS is super painful to me. Actually am a #laravel fan

Thread Thread
 
gabe profile image
Gabe

Really? Interesting - mind sharing what you dislike about it?

Thread Thread
 
__mfarag profile image
Mina Farag

sorry for replying late. but i was away for a while. Actually i hate how OctoberCms messes with the way things organized and functioned. If you were used to MCV pattern and Model-view-controller ways of hierarchy your code, You will find it super weird to deal with so called components and default.htm and so so confusing stuff.

it wasted tons of my hours trying to fix bugs and make it work smoothly for one of my customers. as a senior developer am not recommending it anymore specially for people with a huge laravel background.