Creating a custom helper class in laravel 5.1?

I have followed What is the best practice for creating a custom helper function in php Laravel 5?

This question two answers helps me create my own static class in laravel 5.1. Now my question is, is this class protected or not? because it is a static class. Thank you in advance.

+3


source to share


2 answers


Using a static method in your helper class has nothing to do with securing your application.

The question is why do we even use helper class / methods and that helper class / methods:

Laravel has many helper methods to help you minimize large code writing for common tasks:

This helper class file is located here:

vendor\laravel\framework\src\Illuminate\Foundation\helpers.php

Here are some of the helper methods that Laravel ships with out of the box:

abort

- Throw an HttpException with data.

if (!function_exists('abort')) {
    /**
     * Throw an HttpException with the given data.
     *
     * @param  int     $code
     * @param  string  $message
     * @param  array   $headers
     * @return void
     *
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     */
    function abort($code, $message = '', array $headers = [])
    {
        return app()->abort($code, $message, $headers);
    }
}

      

asset

- Create a resource path for the application.

if (!function_exists('asset')) {
    /**
     * Generate an asset path for the application.
     *
     * @param  string  $path
     * @param  bool    $secure
     * @return string
     */
    function asset($path, $secure = null)
    {
        return app('url')->asset($path, $secure);
    }
}

      

and much more...

So you want to have your own Helper method, perhaps because it is not currently available in Laravel Helpers.



To avoid overriding Laravel helper methods, it is best to use your own helper methods in your class file :

Example. My date helper, which I can reuse in my applications, might look like this:

namespace App\Helpers;

class DateHelper {

    public static function dateFormat1($date) {
        if ($date) {
            $dt = new DateTime($date);

        return $dt->format("m/d/y"); // 10/27/2014
      }
   }
}

      

then you can use it like this:

{{dateHelper::dateFormat1($user->created_at)}}

If we don't want to use the class, we could do this:

//helper method for date
function dateFormat1($date) {
            if ($date) {
                $dt = new DateTime($date);

            return $dt->format("m/d/y"); // 10/27/2014
          }
       }

      

and use it like this:

{{ dateFormat1($user->created_at) }}

However, if subsequent releases of Laravel decide that it has a geppler of the same name dateFormat1

, then a collision or override will occur.

Hence, it's better to put your helper methods in classes.

+5


source


I think you can do as stated. There is nothing wrong with this method. I use it and no problem.



+1


source







All Articles