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.

3
  • 1
    What do you mean by "secured"? And what is a "static class"? Commented Jul 29, 2015 at 9:20
  • @jedrzej.kurylo .i am new to laravel .i have created one helper class .which having static methods .is that ststic methods are secured or not. i mean security issues. Commented Jul 29, 2015 at 9:22
  • No, there is nothing bad about static methods in terms of security. Commented Jul 29, 2015 at 9:28

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

3 Comments

I think you missed the question.
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.
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.