Make your own Helper Functions - Laravel
How Make your own Helper Functions.? Well, we just see how and what should be made.
The structure of the folders that I use to create a "Helper Functions".
[caption id="attachment_391" align="aligncenter" width="248"] helper-functions-blog-whendy[/caption]
Next we create a file "Helpers.php" and saved in the folder "app/Helpers". In this Helpers.php file we put the functions that later we will call / use in our every need.
Example :
This examples of functions that I created to add the active class.
Next, we have registered Helpers.php file that can be used. How to.? The way we have registered at the ServiceProvider.
Ok, Create a file with the name "HelperFunctionProvider.php". Save in the folder "app/Providers".
Follow the example of this code.
I think you understand the purpose of the above code.
Then edit the file config/app.php, add to a providers App\Providers\HelperFunctionProvider :: class .
Well, now you've made Helper Functions themselves.
If you have more simply please leave your comment on Make your own Helper Functions - Laravel. Thank.
The structure of the folders that I use to create a "Helper Functions".
[caption id="attachment_391" align="aligncenter" width="248"] helper-functions-blog-whendy[/caption]
Next we create a file "Helpers.php" and saved in the folder "app/Helpers". In this Helpers.php file we put the functions that later we will call / use in our every need.
Example :
<?php
function AddClassActiveRoute($routes, $output = "active")
{
if(is_array($routes)){
foreach ($routes as $route)
{
if (Route::currentRouteName() == $route) return $output;
}
}
if (Route::currentRouteName() == $routes)
return $output;
}
This examples of functions that I created to add the active class.
Next, we have registered Helpers.php file that can be used. How to.? The way we have registered at the ServiceProvider.
Ok, Create a file with the name "HelperFunctionProvider.php". Save in the folder "app/Providers".
Follow the example of this code.
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperFunctionProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the Helper Function.
*
* @return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
I think you understand the purpose of the above code.
Then edit the file config/app.php, add to a providers App\Providers\HelperFunctionProvider :: class .
Well, now you've made Helper Functions themselves.
If you have more simply please leave your comment on Make your own Helper Functions - Laravel. Thank.
No comments: