如何使用Laravel创建立面类? [英] How do I create a facade class with Laravel?
问题描述
使用Laravel创建立面模型类时遇到了一些问题.我已经关注了 http://laravel.com/docs/facades ,但我想我缺少了一些东西.
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.
我在app/models
中创建了一个名为foo
的文件夹.在那个文件夹中,我有两个文件.
I have created a folder in app/models
called foo
. In that folder I have two files.
第一个文件(Foo.php):
First file (Foo.php):
<?php
namespace Mynamespace;
class Foo {
public function method() {
}
}
?>
第二个文件(FooFacade.php):
Second file (FooFacade.php):
<?php
use Illuminate\Support\Facades\Facade;
class Foo extends Facade {
protected static function getFacadeAccessor() { return 'foo'; }
}
?>
然后我将Foo => 'Mynamespace\Foo'
添加到app/config/app.php
中的aliases
数组中,并运行composer update
和composer dump-autoload
.
Then I added Foo => 'Mynamespace\Foo'
to the aliases
array in app/config/app.php
and ran composer update
and composer dump-autoload
.
现在,当我尝试运行Foo::method()
时,我得到了Non-static method Mynamespace\Foo::method() should not be called statically
.我在做什么错了?
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?
推荐答案
步骤1
在app
文件夹(app/facades
)中创建一个名为facades
的文件夹.
Step 1
Create a folder called facades
in your app
folder (app/facades
).
将立面文件夹添加到您的作曲家自动加载中.
Add the facade folder to your composer autoload.
"autoload": {
"classmap": [
...
"app/facades"
]
},
步骤3
在该文件夹(FooFacade.php
)中创建一个Facade文件并添加以下内容:
Step 3
Create a Facade file in that folder (FooFacade.php
) and add this content:
<?php
use Illuminate\Support\Facades\Facade;
class MyClass extends Facade {
protected static function getFacadeAccessor() { return 'MyClassAlias'; } // most likely you want MyClass here
}
步骤4
在app/models
(MyClass.php
)中创建模型.
Step 4
Create a model in app/models
(MyClass.php
).
<?php
namespace MyNamespace;
use Eloquent; // if you're extending Eloquent
class MyClass extends Eloquent {
...
}
步骤5
创建一个新的服务提供商(您可以在应用程序中创建一个名为serviceproviders
的文件夹并将其添加到composer自动加载中)(app/models/MyClassServiceProvider.php
).
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 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).
将服务提供者添加到config/app.php
中的providers
阵列中.
Add the service provider to the providers
array in config/app.php
.
'providers' => array(
...
'MyServiceProvider'
)
步骤7
运行composer dump
,以便我们可以访问新类.
Step 7
Run composer dump
so we can access our new classes.
您现在可以将MyClassAlias::method()
作为外观访问.
You can now access MyClassAlias::method()
as a facade.
这篇关于如何使用Laravel创建立面类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!