How to create a global function that can be accessed from any controller and click file

I have two file controllers and a backendcontroller. What's the best way to create a global function and access it from both files?

I found here Arian Acosta said useful, but I wonder whether there is the easiest way. Any suggestions would be appreciated.

+3


source to share


6 answers


Create a shared file with any name. Ex. Common.php and write all common function inside it. Now if you want to access this common function in your controller, just include this Common.php file using "use".

use Common;

      



Then you can access all of the common functionality included in Common.php. Do it in the same way in another controller.

+2


source


Decision

One way to do this is to create a class and use an instance of it, this way you can not only access the class object inside a controller, blade or any other class.

AppHelper File

In the app folder create a folder named Helpers and inside it create a filename AppHelper or any of your options

<?php
namespace App\Helpers;

class AppHelper
{
      public function bladeHelper($someValue)
      {
             return "increment $someValue";
      }

     public function startQueryLog()
     {
           \DB::enableQueryLog();
     }

     public function showQueries()
     {
          dd(\DB::getQueryLog());
     }

     public static function instance()
     {
         return new AppHelper();
     }
}

      

Using

In the controller

When in the controller you can call various functions

public function index()
{
    //some code

   //need to debug query
   \App\Helpers\AppHelper::instance()->startQueryLog();

   //some code that executes queries
   \App\Helpers\AppHelper::instance()->showQueries();
}

      



In the click file

Say you were in the click file, this is how you can call the application helper function,

some html code
{{ \App\Helpers\AppHelper::instance()->bladeHelper($value) }}
and then some html code

      

Reduce namespace overhead (optional)

You can also reduce the overhead of calling the full function namespace \ App \ Helpers by creating an alias for the AppHelper class in config \ app.php

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

      

and in your controller or click file you can directly call

\AppHelper::instance()->functioName();

      

+17


source


create a folder Helpers

in your folder app

.

create a file php

named your_helper_function.php

in the folder App\Helpers

.

inside your_helper_function.php

:

<?php

    function your_function($any_parameters){

            //code

        }

?>

      

Then add this to the Files option composer.json

as:

    "files": [

        "app/Helpers/your_helper_function.php",
    ]

      

and finally run this command in terminal,

composer dump-autoload

after that you can access your_function()

anywhere in your laravel project

+3


source


In your Controller.php

, which extends BaseController

, you can create a function like:

public function data($arr = false)
{
 $data['foo'] = 'bar';
 return array_merge($data,$arr);
}

      

And from any controller when you post data to the view;

public function example()
{
 $data['smthg'] = 'smthgelse';
 return view('myView',$this->data($data));
}

      

The data in the primary controller can be accessed from all controllers and blades.

+3


source


Laravel uses default namespaces. Therefore, you need to follow the method described in this answer to install the helper file.

Though in your case you want to access the method in different controllers. There is an easier way for this. Add a method to your base controller app/Http/Controllers/Controller.php

and you can access them in every other controller as it extends it.

// in app/Http/Controllers/Controller.php
protected function dummy()
{
    return 'dummy';
}

// in homecontroller

$this->dummy();

      

+1


source


There are several ways, depending on the specific feature you are trying to add.

1) Create a function inside Controller.php and make all other controllers an extension of that controller. You could handle this with master.blade.php

2) Create a trait, a trait can do a lot for you and save ur controller chips. I personally love to use traits as it will look clean, don't let my Controller.php get messy with many different lines of code.

+1


source







All Articles