Laravel

Laravel 5.2 add custom helper class

Laravel 5.2 add custom helper class

Hi, In this tutorial we are going to see how to add helper class in Laravel.
In this below example we are explaining How to add dynamic active class for your menus.

Lets Assume You have following menus

Link name => Link URL

1. Home => home
2. Page1 => page1
3. Page2 => page2
4. Log in => auth/login
5. Sign up => auth/register

Step 1 :

Create Helper Class.

My Helper Class Path is : app/Helpers/MenuActiveHelper.php

Request::path() => Will provide the entire path
The above function will check the given path is in the Request::path(), If it is it will return as ‘Active’

Step 2 :

Create Service Provider

It will create HelperServiceProvider under app/providers directory.



Step 3 :

Add Below Line in your register() function

Step 4 :

Add the service provider in config/app.php

‘provider’ => [
‘App\Providers\HelperServiceProvider’,
]

‘aliases’ => [
‘isMenuActive’ => App\Helpers\MenuActiveHelper::class,
]

Now You can access the custom function globally.

Step 5 :

How to user it ?

Thats it, Now you will get the active class dynamically.

As like this you can create number of helper classes.
If you like this article, Please don’t forget to share and comment.
Happy Coding!.