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 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
.
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?
It's well explained in that post: http://fideloper.com/create-facade-laravel-4
Hope it helps
A simple Laravel 5 methode:
To create a Facade you need 3 components:
Here's a full example: in the example I'm creating the Facade
ModulesConfig
for theModulesConfigReaderService
class.1) the service class that needs to become accessible via a facade
this is a very normal class
2) the facade required class
simple class extending from the Facade
3) the service provider
a service provider that binds everything together.
USAGE:
1) register the service providers normally
2) access the service class via the facade
Step 1
Create a folder called
facades
in yourapp
folder (app/facades
).Step 2
Add the facade folder to your composer autoload.
Step 3
Create a Facade file in that folder (
FooFacade.php
) and add this content:Step 4
Create a model in
app/models
(MyClass.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
).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 inconfig/app.php
.Step 7
Run
composer dump
so we can access our new classes.Step 8
You can now access
MyClassAlias::method()
as a facade.Step 1: Create Service provider
Step 2: Create Façade class which extends Illuminate\Support\Facades\Facade class.
Step 3: Create the class(TestFacade.php) where you want to add functions.
Step 4: Register service provider and provide alias name in Config\App.php
Call the function Route.php:
Call function in Controller: