Build your First Laravel Helpers
Tags: laravel, php, webdev
Published: 2019-01-31 21:28:02

---
title: Build your First Laravel Helpers
published: true.
description: learn how to build and use laravel helpers.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/dsifpb5jyce4erb8csml.jpg
tags: laravel,php,webdev
---

###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.

<hr width="55%" />

###Let's Set it up.

[1] 
using any IDE - _i use [sublime](https://www.sublimetext.com) 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. 

<hr />
<br />
[2]
let's assume you wanna inject your __layout.blade.php__ template file with [FontAwesome](https://fontawesome.com/icons?d=gallery&m=free) 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](https://fontawesome.com/start)

[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](https://i.pinimg.com/564x/c9/84/b9/c984b9ab6affc907e8771ba76a728402.jpg)

<hr />
<br />
[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](https://i.pinimg.com/originals/51/a3/37/51a337efac7c505665820aee3bfddd8e.gif)

[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_

<hr />
<br />

[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 ?

<br />
[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.