Laravel Alias 找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26527930/
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 10:19:06  来源:igfitidea点击:

Laravel Alias not finding class

phpclasslaravelaliasautoloader

提问by user2834482

I am trying to register an Alias to a class, but Laravel can't find the class, i can reference the class directly in my controller, so i know its loaded correctly.

我正在尝试将别名注册到一个类,但 Laravel 找不到该类,我可以直接在我的控制器中引用该类,所以我知道它正确加载了。

here is my code:

这是我的代码:

    'aliases' => array(
        'FrontendAssets' => 'Lib\FrontendAssets',
    )

the class I am trying to make an alias for

我正在尝试为其创建别名的课程

class FrontendAssets{

    protected static $styles = [];
    protected static $scripts = [];

    public static function styles(){
        return static::assets(static::$styles);
    }

    public static function addStyle($style){
        static::$styles[] = $style;
    }

    public static function scripts(){
        $scripts = static::assets(static::$scripts);
        return $scripts;
    }

    public static function addScript($script){
        static::$scripts[] = $script;
    }

    public static function assets($assets){
        return array_map(function ($asset){
                return $asset;
        }, array_unique($assets));
    }
} 

This is what i am trying to call in my controller

这是我试图在我的控制器中调用的

FrontendAssets::assets();

I have tried adding namespaces but still no joy.

我尝试添加命名空间,但仍然没有乐趣。

If i use \FrontendAssets::assets();in my controller, I can use the class so I know it is defiantly been loaded

如果我\FrontendAssets::assets();在我的控制器中使用,我可以使用该类,所以我知道它已被明确加载

回答by ruuter

php artisan config:clear

Might help also. In case you have previously cached config files with config:cachesince aliases are defined in app config.

也可能有帮助。如果您之前缓存了配置文件,config:cache因为别名是在应用程序配置中定义的。

回答by Marcin Nabia?ek

First run composer dump-autoloadto make sure the class can be found.

首先运行composer dump-autoload以确保可以找到该类。

Now, when you have controller in namespace, for example:

现在,当您在命名空间中有控制器时,例如:

<?php

namespace App\Http\Controllers;

class YourController {

}

if you want to access FrontendAssetsclass you need to add leading backslash, so FrontendAssets::assets();won't work but \FrontendAssets::assets();will work.

如果你想访问FrontendAssets类,你需要添加前导反斜杠,所以FrontendAssets::assets();不会工作但\FrontendAssets::assets();会工作。

You can alternatively import this class:

您也可以导入此类:

<?php

namespace App\Http\Controllers;

use FrontendAssets;

class YourController {

}

and now you will be able to use FrontendAssets::assets();. If it's unclear you might want to look also for explanation at How to use objects from other namespaces and how to import namespaces in PHP

现在您将能够使用FrontendAssets::assets();. 如果不清楚,您可能还想在How to use objects from other namespaces and how to import namespaces in PHP 中寻找解释

回答by Kiran Maniya

Laravel caches the configuration to avoid runtime loading overhead. You have to run one of the given artisan commands,

Laravel 缓存配置以避免运行时加载开销。您必须运行给定的工匠命令之一,

below given command will clear out the configurations cached previously.

下面给定的命令将清除之前缓存的配置。

php artisan config:clear

and the below given command will clear and re-cache the configuration.

下面给出的命令将清除并重新缓存配置。

php artisan config:cache

After clearing the configuration cache, You will be able to user that Alise definde in config/app.php. Hop this helps.

清除配置缓存后,您将能够在config/app.php. 跳这有帮助。