简体   繁体   English

无法自动加载帮助文件 - Laravel 5.3

[英]Can't autoload a helper file - Laravel 5.3

I have a helper file located at 我有一个帮助文件位于

app/Helpers/Navigation.php

Helper file with namespace: 带命名空间的助手文件:

   <?php

namespace App\Helpers;

class Navigation
{
    public static function isActiveRoute($route, $output = 'active')
    {
        if (Route::currentRouteName() == $route) {
            return $output;
        }
    }
}

i wanted to autoload this file . 我想自动加载这个文件。 So in my composer.json i have this: 所以在我的composer.json中我有这个:

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

In my view i want to do this: 在我看来,我想这样做:

<li class="{{ isActiveRoute('main') }}">

But i get the error: 但我得到错误:

Call to undefined function isActiveRoute()

Not sure what i'm doing wrong. 不知道我做错了什么。 I did composer dumpautoload when i changed the composer file. 当我改变作曲家文件时,我做了composer dumpautoload。 I tried installing composer again, that also didn't change anything. 我再次尝试安装composer,也没有改变任何东西。

i had the same issue, i'm assuming that you are using inspinia laravel version, so the problem is that they forgot to remove the file app/Helpers/Navigation.php 我有同样的问题,我假设你正在使用inspinia laravel版本,所以问题是他们忘了删除文件app / Helpers / Navigation.php

if you look the AppServiceProvider they are using the one in '/../Http/Helpers/Helpers.php' 如果您查看AppServiceProvider,他们正在使用'/../Http/Helpers/Helpers.php'中的那个

if you want to do Navigation::isActiveRoute then use the class file 如果你想做Navigation :: isActiveRoute然后使用类文件

but if you want to use {{ isActiveRoute('youRouteName') }} then you need to use the functions in '/../Http/Helpers/Helpers.php' and there is no need to use de composer.json (that was propossed in another solution for another problem) 但是如果你想使用{{ isActiveRoute('youRouteName') }}那么你需要使用'/../Http/Helpers/Helpers.php'中的函数,而不需要使用de composer.json(那个被提出另一个解决另一个问题的方法)

i know i have the same feeling... 我知道我有同样的感觉......

ps: Please sorry about my english ps:请抱歉我的英语

For a helpers file you don't want to be using a class. 对于helpers程序文件,您不希望使用类。 You would jut define the functions you want to use. 您可以直接定义要使用的功能。

Also, it is good practice to wrap your function in a check to make sure that function doesn't already exist. 此外,最好将您的函数包装在一个检查中,以确保该函数尚不存在。

Replace the content of you Naviation.php with: 用以下内容替换Naviation.php的内容:

<?php

if (! function_exists('isActiveRoute')) {

    /**
     * [Description of this function]
     * 
     * @param $route
     * @param string $output
     * @return string
     */
    function isActiveRoute($route, $output = 'active')
    {
        if (Route::currentRouteName() == $route) {
            return $output;
        }
    }
}

Hope this helps! 希望这可以帮助!

When your helper file is a class then there is no need to autoload it. 当您的帮助文件是一个类时,则无需自动加载它。

Just create an alias in config/app.php as: 只需在config/app.php创建一个别名:

'aliases' => [
 ...
    'NavigationHelper' => App\Helpers\Navigation::class,
 ...

Use it in your Blade template as: 在您的Blade模板中使用它:

<li class="{{ NavigationHelper::isActiveRoute('main') }}">

At last, you can remove the following code from composer.json file and the run composer dumpautoload 最后,您可以从composer.json文件和run composer dumpautoload删除以下代码

"files": [
  "app/Helpers/Navigation.php"
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM