Mina Farag
Posted on January 31, 2019
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.
[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.
[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.
Posted on January 31, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 28, 2024