Best Practices for Custom Helpers in Laravel 5

后端 未结 20 2265
暖寄归人
暖寄归人 2020-11-22 06:40

I would like to create helper functions to avoid repeating code between views in Laravel 5:

view.blade.php

Foo Formated text: {{ fo

20条回答
  •  無奈伤痛
    2020-11-22 07:17

    instead of including your custom helper class, you can actually add to your config/app.php file under aliases.

    should be look like this.

     'aliases' => [ 
        ...
        ...
        'Helper' => App\Http\Services\Helper::class,
     ]
    

    and then to your Controller, include the Helper using the method 'use Helper' so you can simply call some of the method on your Helper class.

    eg. Helper::some_function();
    

    or in resources view you can directly call the Helper class already.

    eg. {{Helper::foo()}}
    

    But this is still the developer coding style approach to be followed. We may have different way of solving problems, and i just want to share what i have too for beginners.

提交回复
热议问题