问题
I have two controller file homecontroller and backendcontroller. What is the best way to create global function and access it from both files?
I found here Arian Acosta's answer helpful but I wonder if there is an easiest way. I would appreciate any suggestions.
回答1:
Make a common file with any named. For eg. Common.php and write all the common function within in 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 the common function included in Common.php. Similarly do it in other controller.
回答2:
Solution
One way to do this is to create a class and use its instance, this way you can not only access the object of the class within a controller, blade, or any other class as well.
AppHelper file
In you app folder create a folder named Helpers and within it create a file name AppHelper or any of your choice
<?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();
}
}
Usage
In a controller
When in a controller you can call the 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 a blade file
Say you were in a blade file, here is how you can call the app blade helper function
some html code
{{ \App\Helpers\AppHelper::instance()->bladeHelper($value) }}
and then some html code
Reduce the overhead of namespace (Optional)
You can also reduce the overhead of call the complete function namespace \App\Helpers by creating alias for the AppHelper class in config\app.php
'aliases' => [
....
'AppHelper' => App\Helpers\AppHelper::class
]
and in your controller or your blade file, you can directly call
\AppHelper::instance()->functioName();
回答3:
An Easy Solution:
- Create a new
Helpers
folder in yourapp
directory. - Create a
php
file namedyour_helper_function.php
in thatHelpers
folder. Add your function inside
your_helper_function.php
function your_function($parameters){ //function logic }
Add this file to the
Files
key of yourcomposer.json
like"autoload": { ... "files": [ "app/Helpers/your_helper_function.php" ] ... }
And then finally regenerate composer autoload files. (Run this in your project directory)
composer dump-autoload
That's it! and now you can access your_function()
in any part of your laravel project.
If you want multiple functions to be globally available, you can put them all in a single file and add that file to Files
key of composer.json
like shown in step 4.
If you still have any confusion check my blog post on how to do this:
How to add a Global function in Laravel using Composer?
回答4:
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 send a data to a view;
public function example()
{
$data['smthg'] = 'smthgelse';
return view('myView',$this->data($data));
}
The data in the the main controller can be accessed from all controllers and blades.
回答5:
Laravel uses namespaces by default. So you need to follow the method described in that answer to setup a helper file.
Though in your case you want to access a method in different controllers. For this there's a simpler way. Add a method to you base controller app/Http/Controllers/Controller.php
and you can access them in every other controller since they extend it.
// in app/Http/Controllers/Controller.php
protected function dummy()
{
return 'dummy';
}
// in homecontroller
$this->dummy();
回答6:
There are a few ways, depending on the exact functionality you're trying to add.
1) Create a function inside Controller.php, and make all other controller extend that controller. You could somewhat compair this to the master.blade.php
2) Create a trait, a trait can do a lot for you, and keeping ur controllers clean. I personally love to use traits as it will look clean, keep my Controller.php from being a mess with tons of different lines of code.
来源:https://stackoverflow.com/questions/44021662/how-to-create-global-function-that-can-be-accessed-from-any-controller-and-blade