X
    Categories: 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

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <ul class="nav navbar-nav">
      <li><a href="/home">Home</a></li>
      <li><a href="/page1">Page 1</a></li>
      <li><a href="/page2">Page 2</a></li> 
    </ul>
    <ul class="nav navbar-nav navbar-right">
      @if (Auth::guest())
        <li>
          <a href="/auth/register"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
        <li>
          <a href="/auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      @endif
    </ul>
  </div>
</nav>

Step 1 :

Create Helper Class.

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

<?php
namespace App\Helpers;

use Illuminate\Support\Str;
use Request;

Class MenuActiveHelper {

	 /*
      Sets active class on navigation.
    */
    public static function setActive($path, $active = 'active') {
      return  Str::contains(Request::path(), $path) ? $active : '';
    }

}

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

php artisan make:provider HelperServiceProvider

It will create HelperServiceProvider under app/providers directory.


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Step 3 :

Add Below Line in your register() function

/**
* Register the application services.
*
* @return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}

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 ?

<ul class="nav navbar-nav navbar-right">
@if (Auth::guest())
<li class="{{ isMenuActive::setActive('auth/register') }}" >
<a href="{!! URL::action('Auth\\AuthController@getRegister') !!}"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li class="{{ isMenuActive::setActive('auth/login') }}">
<a href="{!! URL::action('Auth\\AuthController@getLogin') !!}"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
@else
<li><a href="{!! URL::action('Auth\\AuthController@logout') !!}"><span class="glyphicon glyphicon-log-in"></span> Logout</a></li>
@endif
</ul>

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

Marimuthu: