如何使用 Laravel 创建外观类?

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

How do I create a facade class with Laravel?

phplaravelfacade

提问by Marwelln

I'm having a little problem with creating a facade model class with Laravel. I have followed http://laravel.com/docs/facadesbut I guess I'm missing something.

我在使用 Laravel 创建外观模型类时遇到了一些问题。我已经关注了http://laravel.com/docs/facades,但我想我错过了一些东西。

I have created a folder in app/modelscalled foo. In that folder I have two files.

我在app/models名为foo. 在那个文件夹中,我有两个文件。

First file (Foo.php):

第一个文件(Foo.php):

<?php
namespace Mynamespace;

class Foo {
    public function method() {

    }
}
?>

Second file (FooFacade.php):

第二个文件(FooFacade.php):

<?php
use Illuminate\Support\Facades\Facade;

class Foo extends Facade {
    protected static function getFacadeAccessor() { return 'foo'; }
}
?>

Then I added Foo => 'Mynamespace\Foo'to the aliasesarray in app/config/app.phpand ran composer updateand composer dump-autoload.

然后我添加Foo => 'Mynamespace\Foo'aliases数组中app/config/app.php并运行composer updatecomposer dump-autoload

Now when I try to run Foo::method()I get Non-static method Mynamespace\Foo::method() should not be called statically. What am I doing wrong?

现在,当我尝试运行时,Foo::method()我得到Non-static method Mynamespace\Foo::method() should not be called statically. 我究竟做错了什么?

回答by Marwelln

Step 1

第1步

Create a folder called facadesin your appfolder (app/facades).

facades在您的app文件夹 ( app/facades) 中创建一个名为的文件夹。

Step 2

第2步

Add the facade folder to your composer autoload.

将外观文件夹添加到您的 Composer 自动加载。

"autoload": {
    "classmap": [
        ...
        "app/facades"
    ]
},

Step 3

第 3 步

Create a Facade file in that folder (FooFacade.php) and add this content:

在该文件夹 ( FooFacade.php) 中创建一个 Facade 文件并添加以下内容:

<?php
use Illuminate\Support\Facades\Facade;

class MyClass extends Facade {
    protected static function getFacadeAccessor() { return 'MyClassAlias'; } // most likely you want MyClass here
}

Step 4

第四步

Create a model in app/models(MyClass.php).

app/models( MyClass.php) 中创建模型。

<?php
namespace MyNamespace;

use Eloquent; // if you're extending Eloquent

class MyClass extends Eloquent {
    ...
}

Step 5

第 5 步

Create a new service provider (you can create a folder in app called serviceprovidersand add it to composer autoload) (app/models/MyClassServiceProvider.php).

创建一个新的服务提供者(您可以在 app 中创建一个名为的文件夹serviceproviders并将其添加到 composer autoload)(app/models/MyClassServiceProvider.php)。

<?php
use Illuminate\Support\ServiceProvider;

class MyClassServiceProvider extends ServiceProvider {
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        $this->app->bind('MyClassAlias', function(){
            return new MyNamespace\MyClass;
        });
    }
}

Here you can add new binding if you want another facade (don't forget to create a facade file if so).

如果你想要另一个外观,你可以在这里添加新的绑定(如果是这样,不要忘记创建一个外观文件)。

Step 6

第 6 步

Add the service provider to the providersarray in config/app.php.

服务提供商添加到providers阵列config/app.php

'providers' => array(
    ...
    'MyServiceProvider'
)

Step 7

第 7 步

Run composer dumpso we can access our new classes.

运行composer dump以便我们可以访问我们的新类。

Step 8

第 8 步

You can now access MyClassAlias::method()as a facade.

您现在可以MyClassAlias::method()作为外观访问。

回答by frenus

It's well explained in that post: http://fideloper.com/create-facade-laravel-4

那篇文章中有很好的解释:http: //fideloper.com/create-facade-laravel-4

Hope it helps

希望能帮助到你

回答by Mahantesh Kumbar

Step 1: Create Service provider

第 1 步:创建服务提供者

<?php
    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    class NewFacadeServiceProvider extends ServiceProvider{
       public function register(){
           $this->app->singleton('TestFacades',function() {
            //'TestFacades' alias name for the fa?ade class
               return new \App\TestFacade;
           });
       }
    }

Step 2: Create Fa?ade class which extends Illuminate\Support\Facades\Facade class.

第 2 步:创建扩展 Illuminate\Support\Facades\Facade 类的 Fa?ade 类。

<?php
    namespace App\Facade; //created 'facade' folder in app directory
    use Illuminate\Support\Facades\Facade;
    class TestFacade extends Facade{
        protected static function getFacadeAccessor() { 
            return 'TestFacades'; //'TestFacades' alias name for the fa?ade class declare in the class 'NewFacadeServiceProvider'
        } 
    }

Step 3: Create the class(TestFacade.php) where you want to add functions.

第 3 步:创建要添加函数的类(TestFacade.php)。

<?php
    namespace App;
    class TestFacade{
        public function dummy(){
            return "Business Logic ";
        }   
    }

Step 4: Register service provider and provide alias name in Config\App.php

第四步:在Config\App.php中注册服务提供者并提供别名

'providers' => [ //...
     App\Providers\NewFacadeServiceProvider::class
 ],

 //Class Aliases
 'aliases' => [ //...
    'FacadeTester' => App\Facade\TestFacade::class,
 ]

Call the function Route.php:

调用函数 Route.php:

Route::get('/skull',function(){
    return FacadeTester::dummy();
});

Call function in Controller:

控制器中的调用函数:

return \FacadeTester::dummy();

回答by Mahmoud Zalt

A simple Laravel 5 methode:

一个简单的 Laravel 5 方法:

To create a Facade you need 3 components:

要创建 Facade,您需要 3 个组件:

  • The wanna be Facade Class, the class that needs to become accessible via facade.
  • The Facade required Class.
  • The Service Provider, which registers the Facade class in the App container
  • 想要成为 Facade 类,需要通过 Facade 访问的类。
  • 门面所需的类。
  • Service Provider,在App容器中注册Facade类

Here's a full example: in the example I'm creating the Facade ModulesConfigfor the ModulesConfigReaderServiceclass.

这是一个完整的示例:在示例中,我正在ModulesConfigModulesConfigReaderService类创建 Facade 。

1) the service class that needs to become accessible via a facade

1) 需要通过 Facade 访问的服务类

<?php

namespace Hello\Services\Configuration\Portals;

use Illuminate\Support\Facades\Config;

class ModulesConfigReaderService
{

    public function getSomething()
    {
        return 'Whatever';
    }

}

this is a very normal class

这是一堂很普通的课

2) the facade required class

2)门面所需的类

<?php

namespace Hello\Services\Configuration\Facade;

use Illuminate\Support\Facades\Facade;

class ModulesConfig extends Facade
{

    protected static function getFacadeAccessor()
    {
        return 'modulesConfigReaderService';
    }

}

simple class extending from the Facade

从 Facade 扩展的简单类

3) the service provider

3)服务提供者

<?php

namespace Hello\Services\Configuration\Providers;

use Hello\Modules\Core\Providers\Abstracts\ServiceProvider;

class ModulesConfigServiceProvider extends ServiceProvider
{

    public function register()
    {
        $this->app->bind('modulesConfigReaderService', function(){
            return $this->app->make('Hello\Services\Configuration\Portals\ModulesConfigReaderService');
        });
    }
}

a service provider that binds everything together.

将所有内容绑定在一起的服务提供者。

USAGE:

用法:

1) register the service providers normally

1) 正常注册服务提供者

2) access the service class via the facade

2) 通过门面访问服务类

$whatever = ModulesConfig::getSomething();