如何在Lumen 8.0中使用Helper类? [英] How to use a Helper class in Lumen 8.0?

查看:113
本文介绍了如何在Lumen 8.0中使用Helper类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人可以建议我如何在Lumen 8.0版本中使用帮助器?非常感谢.

Is there anyone who can suggest me how can we use helper in Lumen 8.0 version? Thanks a lot.

推荐答案

我按照以下步骤在流明8.0中的项目中添加了辅助功能:

I have followed below steps to add helper feature in my project in Lumen 8.0:

第一步:我添加了"app/Helpers/MasterFunctionsHelper.php"自动加载"下的字符串->文件" composer.json 文件中的数组.这里的"MasterFunctionsHelper"是我的助手名称:

First Step: I have added "app/Helpers/MasterFunctionsHelper.php" string under "autoload" -> "files" array in composer.json file. Here "MasterFunctionsHelper" is my helper name:

"autoload": {
        "files": [
            "app/Helpers/MasterFunctionsHelper.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },

第二步:我创建了"MasterFunctionsHelper.php"文件位于"app/Helpers/MasterFunctionsHelper.php".如果助手"目录不存在,请在"app"目录中创建目录.

Second step: I have created "MasterFunctionsHelper.php" file at "app/Helpers/MasterFunctionsHelper.php". If "Helpers" directory is not exist then please create it in "app" directory.

第三步:在"MasterFunctionsHelper.php"中创建了一个类文件:

Third step: created a class in "MasterFunctionsHelper.php" file:

<?php

namespace App\Helpers;

class MasterFunctionsHelper{

    public static function sayhello()
    {
        return "Hello Friends";
    }

}

第四步:打开控制器文件,例如:"UsersController.php"并包含使用App \ Helpers \ MasterFunctionsHelper;";然后称为"MasterFunctionsHelper :: sayhello();"助手类的功能为:

Forth step: Opened controller file eg: "UsersController.php" and included "use App\Helpers\MasterFunctionsHelper;" and then called "MasterFunctionsHelper::sayhello();" function of helper class as:

<?php

namespace App\Http\Controllers;

use App\Helpers\MasterFunctionsHelper;

class UsersController extends Controller
{
    public function index()
    {
        echo MasterFunctionsHelper::sayhello();
    }

}

第五步:打开命令行并在命令下方运行:

Fifth step: Opened command line and run below command:

composer dump-autoload

当我运行索引"时,UsersController"的动作在浏览器中,然后输出为:

When I run "index" action of "UsersController" in the browser then output was as:

你好朋友

对我有用.希望对您有帮助.谢谢.

It worked for me. I hope, this will also help you. Thank you.

这篇关于如何在Lumen 8.0中使用Helper类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆