Write a View Helper in Laravel 5 with best practices

In my last blog post I talked about deploying Laravel 5 application in a shared hosting. Still I am working in the same project. This is pretty much a big project that I am working alone right now. Along with this project, I am also working long hours on upgrading my business, after I read a quote by Tristan Harris the Co-Founder & Executive Director of Center for Humane Technology, describing the remarkable use of AI in business on the Salesforce website. Nevertheless, let’s talk about the main point. Recently, I had to write a view helpers and now I will share my experience writing it.
index
A little bit view helper?
In my own way, View helper could be a class or a function that will help you to reduce writing complex and redundant view-processing logics in your view script. According to the best practicez it’s very bad to write any business logic or even simple logic in view. View will contains only view related codes.

Okay come back to Laravel world. In Laravel framework, I didn’t get any official documentation for writing the view helpers. Although, I got something in forum but that is kind of manual ways. Let me share the code.

You can write a script in app/Helpers/currencyname.php, and then autoload that in composer.

<?php 
 
function addCurrencyNamebyCountry($countryName) 
{
  switch ($countryName) {
     case 'usa':
          echo "US Dollar";
          break;
      case 'europe':
          echo "Euro";
          break;
      case 'canada':
          echo "CAD";
          break;
     case 'british':
          echo "Pound";
          break;
      default:
       echo "Euro";
  }
}

Add to composer.json file and dump the autoloader

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Helpers/currencyname.php" // <---- ADD THIS
    ]
},

Then, dump the autoload

$ composer dump-auto

You can write on the above way but I already said this is kind of manual way to me. If you want to add another helpers and you will have to add again to composer and will have to dump the composer autoload.

Lets write it in smart way…. You have already written the view helper at app/Helpers/currencyname.php. Now, you will write a class typed Service Provider. This is not only for the view, you can use anywhere across your application.

Put the class at app/Providers/HelperServiceProvider.php

<?php namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
 
class HelperServiceProvider extends ServiceProvider {
 
	/**
	 * Bootstrap any application services.
	 *
	 * @return void
	 */
	public function boot()
	{ }
 
	/**
	 * Register any application services.
	 *
	 * This service provider is a great spot to register your various container
	 * bindings with the application. As you can see, we are registering our
	 * "Registrar" implementation here. You can add your own bindings too!
	 *
	 * @return void
	 */
	public function register()
        {
          foreach (glob(app_path().'/Helpers/*.php') as $filename)
          {
              require_once($filename);
          }
        }
 
}

And register the service provider in your config/app.php in the end of providers array.

'providers' => [
    'App\Providers\HelperServiceProvider',
]

Done. Now, you can just add script at app/helpers folder. All helper functions will be available in view. Happy Clean Code 🙂

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read previous post:
Deploy your Laravel 5 App in shared hosting

Once again, I had to make my hand dirty. This time I had to deploy Laravel 5 application in a...

Close