如何创建可从任何控制器和刀片文件访问的全局功能 [英] how to create global function that can be accessed from any controller and blade file
问题描述
我有两个控制器文件homecontroller和backendcontroller.创建全局功能并从两个文件访问全局功能的最佳方法是什么?
I have two controller file homecontroller and backendcontroller. What is the best way to create global function and access it from both files?
我在在这里找到阿里安·阿科斯塔(Arian Acosta)的回答很有帮助,但我想知道是否有最简单的方法.任何建议,我将不胜感激.
I found here Arian Acosta's answer helpful but I wonder if there is an easiest way. I would appreciate any suggestions.
推荐答案
使用任何命名文件创建公用文件.例如. Common.php并在其中写入所有通用功能.现在,如果您想在控制器中访问此常用功能,只需使用"use"添加此Common.php文件即可.
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;
然后,您可以访问Common.php中包含的所有常用功能.类似地,在其他控制器中执行该操作.
Then you can access all the common function included in Common.php. Similarly do it in other controller.
这篇关于如何创建可从任何控制器和刀片文件访问的全局功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!