4

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.

scott
  • 3,112
  • 19
  • 52
  • 90

3 Answers3

6

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.

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
1

I think you can do as specified. There is nothing wrong with this method. I'm using it to, and no trouble.

LeNiglo
  • 71
  • 8
0

You can follow the simple steps below for creating Helper in Laravel 5.x

  • Step 1

    Create new helper_name.php file in app/Helpers directory Ex. I have created the DemoHelper.php in this way app/Helpers/DemoHelper.php

  • Step 2

    Add the entry of created Helper(DemoHelper.php) to composer.json file in autoload section

    "autoload": {
     "files": [
         "app/Helpers/Helper.php",
         "app/Helpers/DemoHelper.php"
     ]
    },
    
  • Step 3

    Finaly, composer dump-autoload hit this command in terminal.

Rahul Hirve
  • 1,109
  • 13
  • 20
  • I think you missed the question. – Don't Panic Jul 17 '18 at 17:08
  • Hi @Don't Panic, Best way to write helpers without class, Writing classes and importing classes in helper is bad practice. So simply normal repeatedly used calculation/ logic functions you can put in helper. For this requirement you can use traits this is good practice. – Rahul Hirve Jul 18 '18 at 02:42
  • OP included a link to the question showing how he created his helper. Your answer just repeats what is in that other question. So you are showing him how to do what he already did. But the point of my comment was that this was not even what OP asked (obviously - he already did it). – Don't Panic Jul 18 '18 at 09:33