Support the ongoing development of Laravel.io →
Installation
Last updated 1 year ago.
0

Your question is look like Best practices for custom helpers on Laravel 5. No worries..! let check in brief.

I think you requesting for helper function. You can place you php function body at single file and you can use this through the site. Let's we check it step by step:

1) Create Helper.php to wrap your helper(PHP) function(s) under app/Helpers/. So your Helper.php code like:

namespace App\Helpers;

class Helper
{
    public static function hello()
    {
        return "Hello..!";
    }
}

2) Add alias in config/app.php

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

3) Use helper function hello() in blade file:

{!! Helper::hello() !!}{{--This will print 'hello' from hello() helper method}}

4) You can use helper function into controller like:

namespace App\Http\Controllers;

use App\Helpers\Helper;

class MyController extends Controller
{

    public function __construct()
    {
        Helper::hello();
    }
}   

You can get more idea from stack answer Best practices for custom helpers on Laravel 5 taken help from same.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.