Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating custom helper class?

Tags:

php

laravel

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

This question two answers help me to create custom static class in laravel 5.1 .now my question is whether that class is secured or not ? because it is a static class . Thank you in advance.

like image 850
scott Avatar asked Mar 16 '23 06:03

scott


1 Answers

Using 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 what are helper class/methods:

Laravel has many helper methods which helps you to minimize writing to much code for common tasks:

This helper class file is located here:

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

These are some of the helper methods that comes with Laravel out-of-box:

abort - Throw an HttpException with the given 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 - Generate an asset 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 lots more...

So you wish to have your own Helper method, maybe because its not currently available in Laravel Helpers.

To avoid overriding Laravel helper methods, Its better you put your own helper methods in a class file:

Example: My helper class for Dates 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 could use it like so:

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

If we don't wish to use a class, we could have done 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, what if later releases of Laravel decides to have a hepler with same name dateFormat1 then there will be a collision or overrides.

Hence its better to put you helper methods in classes.

like image 156
Emeka Mbah Avatar answered Mar 24 '23 04:03

Emeka Mbah