简体   繁体   English

如何使用Laravel创建外观类?

[英]How do I create a facade class with Laravel?

I'm having a little problem with creating a facade model class with Laravel. 我在使用Laravel创建一个Facade模型类时遇到了一些问题。 I have followed http://laravel.com/docs/facades but I guess I'm missing something. 我已经关注了http://laravel.com/docs/facades,但我想我错过了一些东西。

I have created a folder in app/models called foo . 我在app/models创建了一个名为foo的文件夹。 In that folder I have two files. 在该文件夹中,我有两个文件。

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 aliases array in app/config/app.php and ran composer update and composer dump-autoload . 然后我将Foo => 'Mynamespace\\Foo'app/config/app.phpaliases数组中,并运行了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 . 现在,当我尝试运行Foo::method()我得到Non-static method Mynamespace\\Foo::method() should not be called statically What am I doing wrong? 我究竟做错了什么?

Step 1 第1步

Create a folder called facades in your app folder ( app/facades ). 在您的app文件夹( app/facades )中创建一个名为facades文件夹。

Step 2 第2步

Add the facade folder to your composer autoload. 将facade文件夹添加到composer autoload。

"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 serviceproviders and 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 providers array in config/app.php . 将服务提供者添加到config/app.phpproviders数组中。

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

Step 7 第7步

Run composer dump so we can access our new classes. 运行composer dump以便我们可以访问我们的新类。

Step 8 第8步

You can now access MyClassAlias::method() as a facade. 您现在可以访问MyClassAlias::method()作为外观。

It's well explained in that post: http://fideloper.com/create-facade-laravel-4 那篇帖子中有很好的解释: http//fideloper.com/create-facade-laravel-4

Hope it helps 希望能帮助到你

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步:创建Façade类,扩展Illuminate \\ Support \\ Facades \\ Facade类。

<?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 步骤4:注册服务提供商并在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();

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. 想成为Facade Class,需要通过外观进入的类。
  • The Facade required Class. Facade需要Class。
  • The Service Provider, which registers the Facade class in the App container 服务提供程序,它在App容器中注册Facade类

Here's a full example: in the example I'm creating the Facade ModulesConfig for the ModulesConfigReaderService class. 这是一个完整的示例:在示例中,我正在为ModulesConfigReaderService类创建Facade ModulesConfig

1) the service class that needs to become accessible via a facade 1)需要通过外观访问的服务类

<?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();

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

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