Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 989
Marwelln Avatar asked Jun 06 '13 08:06

Marwelln


People also ask

What is facade class in Laravel?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

What does Laravel use for seeding & facades?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

What is PHP facade?

Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework. While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.


Video Answer


3 Answers

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

<?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 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

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.

like image 176
Marwelln Avatar answered Oct 20 '22 02:10

Marwelln


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

Hope it helps

like image 42
frenus Avatar answered Oct 20 '22 01:10

frenus


Step 1: Create Service provider

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

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

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

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

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

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

Call the function Route.php:

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

Call function in Controller:

return \FacadeTester::dummy();
like image 1
Mahantesh Kumbar Avatar answered Oct 20 '22 01:10

Mahantesh Kumbar