Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

php - How do I create a facade class with Laravel?

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

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

First file (Foo.php):

<?php
namespace Mynamespace;

class Foo {
    public function method() {

    }
}
?>

Second file (FooFacade.php):

<?php
use IlluminateSupportFacadesFacade;

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

Then I added Foo => 'MynamespaceFoo' to the aliases array in app/config/app.php and ran composer update and composer dump-autoload.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Step 1

Create a folder called facades in your app folder (app/facades).

Step 2

Add the facade folder to your composer autoload.

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

Step 3

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

<?php
use IlluminateSupportFacadesFacade;

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).

<?php
namespace MyNamespace;

use Eloquent; // if you're extending Eloquent

class MyClass extends Eloquent {
    ...
}

Step 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).

<?php
use IlluminateSupportServiceProvider;

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

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

Step 6

Add the service provider to the providers array in config/app.php.

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

Step 7

Run composer dump so we can access our new classes.

Step 8

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...