辅助函数未加载 laravel 5 - 调用未定义的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29710566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:22:26  来源:igfitidea点击:

helper functions not loading laravel 5 - Call to undefined function

phplaravelcomposer-phplaravel-5

提问by n1k41l

I've been having a problem loading my helpers file in Laravel 5 and calling the functions from views. The below is a quick overview of how I attempted to setup helpers within my L5 Project.

我在 Laravel 5 中加载我的帮助文件并从​​视图调用函数时遇到问题。下面是我如何尝试在我的 L5 项目中设置助手的快速概述。

  1. Created a helpers.php file within app/Http;
  2. The helpers file contains a basic function, to produce the page title.

    public function full_title($title)
    {
         $base_title = 'Social Tracking Application';
    
         if (isset($title) && $title != ''){
            $comp_title = $title . " | " .  $base_title;    
         } else {
           $comp_title = $base_title;
         }
    
    return $comp_title;
    }
    
  3. Updated composer.json to autoload the new helpers file.

    "autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files":["app/Http/helpers.php"]
    

    },

  4. Run composer dump-autoload from the terminal.

  1. 在 app/Http 中创建了一个 helpers.php 文件;
  2. helpers 文件包含一个基本功能,用于生成页面标题。

    public function full_title($title)
    {
         $base_title = 'Social Tracking Application';
    
         if (isset($title) && $title != ''){
            $comp_title = $title . " | " .  $base_title;    
         } else {
           $comp_title = $base_title;
         }
    
    return $comp_title;
    }
    
  3. 更新了 composer.json 以自动加载新的帮助文件。

    "autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files":["app/Http/helpers.php"]
    

    },

  4. 从终端运行 composer dump-autoload。

After these steps I was under the impression that I could use my helper from views and controllers. So I placed a call to my new helper function within my main layout view by using the following:

在这些步骤之后,我的印象是我可以从视图和控制器中使用我的助手。因此,我使用以下命令在主布局视图中调用了新的辅助函数:

<title> full_title($title)  </title>

However when I navigate to any page I receive a FatalErrorMessage Call to Undefined Function full_title. (note the message is usually in the title bar so I move it to a body tag to view the error message).

但是,当我导航到任何页面时,我会收到一个 FatalErrorMessage Call to Undefined Function full_title。(请注意,消息通常位于标题栏中,因此我将其移至正文标记以查看错误消息)。

I can't figure out what it is I'm doing wrong here, I've seen other examples of people using custom helper files in a similar manner but mine just doesn't seem to work am I missing something obvious?

我不知道我在这里做错了什么,我见过其他人以类似方式使用自定义帮助文件的例子,但我的似乎不起作用,我错过了一些明显的东西吗?

I'm running this of Homestead.

我正在运行 Homestead 的这个。

full code base: https://github.com/n1k41l/SocialTrak/tree/middleware-currentUser

完整代码库:https: //github.com/n1k41l/SocialTrak/tree/middleware-currentUser

回答by n1k41l

I've managed to solve the problem myself, the issue was that I created my Helpers file as a php class. The helpers file should only be a php file - with functions not a class with functions... (obvious mistake once I found it :S).

我已经设法自己解决了这个问题,问题是我将 Helpers 文件创建为 php 类。helpers 文件应该只是一个 php 文件 - 带有函数而不是带有函数的类......(一旦我发现它就明显错误:S)。

回答by Ben Wilson

You may need to use the class name in front of the function when calling it in your view or controller, e.g.:

在视图或控制器中调用函数时,您可能需要在函数前面使用类名,例如:

Helper::my_function()

回答by Xedecimal

I found I had this problem too at some point, in which I had put my helpers under app/Helpers/functions.php instead of app/Http/helpers.php. Moving them to the latter made everything start working fine.

我发现我在某个时候也有这个问题,我把我的助手放在 app/Helpers/functions.php 而不是 app/Http/helpers.php。将它们移到后者使一切开始正常工作。