自定义类中的 Laravel 4 别名

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

Laravel 4 Aliases in custom classes

laravellaravel-4

提问by Leonel Franchelli

i want to use the alias classes on laravel 4 "facades" like App::method , Config::method.

我想在laravel 4“facades”上使用别名类,如 App::method 、 Config::method 。

Well the thing is that i create a custom class and i have to import the namespaces like

好吧,问题是我创建了一个自定义类,我必须导入命名空间,例如

<?php

namespace Face\SocialHandlers;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;

class FacebookHandler implements SocialHandlerInterface {

    public function registrar($perfil) {
        Config::get('facebook');
    }

}

is there any way to use those classes like in controllers or routes files of the framework ?

有没有办法像在控制器或框架的路由文件中那样使用这些类?

like

喜欢

<?php

namespace Face\SocialHandlers;

//use Illuminate\Support\Facades\App;
//use Illuminate\Support\Facades\Config;

class FacebookHandler implements SocialHandlerInterface {

    public function registrar($perfil) {
        Config::get('facebook');
    }

}

Cya

氰化物

ps: sry for my english

ps:sry我的英语

回答by Sergiu Paraschiv

You can use use Config;instead of the more verbose use Illuminate\Support\Facades\Config;and the autoloader should handle it correctly.

您可以使用use Config;而不是更详细的use Illuminate\Support\Facades\Config;,并且自动加载器应该正确处理它。

回答by demve

Just as a tip, you shouldn't hardcode dependencies in your code. Instead of using the facades, you could create an "ConfigInterface"to get the common dependencies you need. Then create a "LaravelConfig class"(Or Laravel4Config.php)and implement those methods.

作为提示,您不应该在代码中硬编码依赖项。您可以创建一个"ConfigInterface"来获取所需的公共依赖项,而不是使用外观。然后创建一个"LaravelConfig class"(或 Laravel4Config.php)并实现这些方法。

For a Quick Fix Answer, "catch the underliying facade instance":

对于快速修复答案,“捕获底层外观实例”:

namespace Face\SocialHandlers;

//use Illuminate\Support\Facades\App;
//use Illuminate\Support\Facades\Config;

class FacebookHandler implements SocialHandlerInterface {
    protected $config;
    protected $app;

    public function __construct()
    {
        $this->config = \Illuminate\Support\Facades\Config::getFacadeRoot();
        $this->app = \Illuminate\Support\Facades\App::getFacadeRoot();
    }

    public function registrar($perfil) {
        $this->config->get('facebook');
    }

}

For a Real Answer, maybe tedious, but good in the long run, instead of using the facades use an interface.

对于真正的答案,可能很乏味,但从长远来看是好的,而不是使用外观使用接口。

interface SocialConfigInterface
{
    public function getConfigurationByKey($key)
}

Then

然后

class Laravel4Config implements SocialConfigInterface
{
    protected $config;

    public function __construct()
    {
        $this->config = \Illuminate\Support\Facades\Config::getFacadeRoot(); //<-- hard coded, but as expected since it's a class to be used with Laravel 4
    }

    public function getConfigurationByKey($key)
    {
        return $this->config->get($key);
    }
}

And Your Code

和你的代码

namespace Face\SocialHandlers;

//use Illuminate\Support\Facades\App;
//use Illuminate\Support\Facades\Config;

class FacebookHandler implements SocialHandlerInterface {
    protected $config;

    public function __construct(SocialConfigInterface $config)
    {
        $this->config = $config;
    }

    public function registrar($perfil) {
        $this->config->get('facebook');
    }

}

This way, if you want to change between frameworks you just need to create a SocialConfigInterfaceImplementation, or imagine the scenario where Laravel 5 wont use Facades, you want your code to be independent of "outsider changes" this is inversion of control IoC

这样,如果您想在框架之间进行更改,您只需要创建一个SocialConfigInterface实现,或者想象一下 Laravel 5 不会使用 Facades 的场景,您希望您的代码独立于“外部更改”,这就是控制反转 IoC

回答by devo

First run,

第一次运行,

php artisan dump-autoload

This will add your class namespace to vendor/composer/autoload_classmap.php. Now locate the entry for your class in this classmap array and get the proper namespace from there.

这会将您的类命名空间添加到vendor/composer/autoload_classmap.php. 现在在这个类映射数组中找到您的类的条目并从那里获取正确的命名空间。

For example, you will get something like,

例如,你会得到类似的东西,

'Illuminate\Support\Facades\App' => $vendorDir . '/laravel/framework/src/Illuminate/Support/Facades/App.php',

For this particular entry you have an alias in app/config/app.php

对于此特定条目,您有一个别名 app/config/app.php

'App'             => 'Illuminate\Support\Facades\App',

At the same way, locate your entry and use an alias in app/config/app.php.

同样,找到您的条目并在app/config/app.php.

回答by Dennis Braga

You just have to create a folder, or place a class wherever already is listed for autoload. Me, for exemple, have this class PDFMaker, that uses a DomPDF Laravel implementation. I created a folder named librariesand put the path to it (under the appfolder) in the autoload:classmapkey on composer.json

你只需要创建一个文件夹,或者在已经列出自动加载的地方放置一个类。例如,我有这个PDFMaker使用 DomPDF Laravel 实现的类。我创建了一个名为的文件夹libraries并将其路径(在app文件夹下)autoload:classmap放在composer.json

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/libraries",
        "app/models",
        "app/helpers",
        "app/database/migrations"
    ]

I did the same with commandsfor artisan commands! When you do that, you only have to declare a new object for any class under that folder, or call it in a static way, if the class has defined static methods. Something like Class::method.

commands对工匠命令做了同样的事情!当您这样做时,您只需为该文件夹下的任何类声明一个新对象,或者以静态方式调用它,如果该类定义了静态方法。类似的东西Class::method

Hope it helps you, have a nice day! :D

希望对你有帮助,祝你有美好的一天!:D

EDIT: After that, don't forget the dump-autoloadfor placing the new class in autoload scope.

编辑:在那之后,不要忘记dump-autoload将新类放置在自动加载范围内。

EDIT 2: Remember that once you've put the class on autoload, it will be in same scope the others, so you won't have to import it to other, neither others to it!

编辑 2:请记住,一旦您将类置于自动加载状态,它就会与其他类处于同一范围内,因此您不必将其导入其他类,也不必将其他类导入它!

回答by Jeroen Noten

You can also prefix the class names with a backslash, to use the global namespace: \Config::get('facebook')and \App::someMethod()will work without the need to add a usestatement, regardless of the file's namespace.

您还可以使用反斜杠作为类名的前缀,以使用全局命名空间:\Config::get('facebook')并且\App::someMethod()无需添加use语句即可工作,无论文件的命名空间如何。