How to create your own facade in Laravel 4

Looked at a few facade guides and laravel 4 ... tried some ... didn't like the way they work.

For example, they do not all provide a way to determine where to store facade and service provider files ... and I tried to move away from this and got my head bumped into several walls until I decided to make this stream.

So: let's say I have an application called Laracms (laravel cms).

I want to save everything that I create - facades, service providers, etc. in the folder under the application named laracms.

So I would have had / app / laracms / facades, / app / laracms / serviceproviders, etc. I donโ€™t want to mix facades with database models, I want things to be as separate as possible.

Now let's take, in my case, the parameter name for the facade (I want to implement a settings class for use in views and an administrator to configure different types).

Settings :: get (), Settings :: set () as methods.

Can someone explain how to install facades correctly? I do not know what I am doing wrong, and I need a new beginning.

Thank you, Chris

Look for simple, step-by-step explanations of how and why.

+6
source share
2 answers

First you need to go to the app/config/app.php and add to providers :

 'Laracms\Providers\SettingsServiceProvider', 

In the same file in the aliases section you should add:

  'Settings' => 'Laracms\Facades\Settings', 

In app/Laracms/Providers you must create the SettingsServiceProvider.php file

 <?php namespace Laracms\Providers; use Illuminate\Support\ServiceProvider; class SettingsServiceProvider extends ServiceProvider { public function register() { $this->app->bind('settings', function() { return new \Laracms\Settings(); }); } } 

In app/Laracms/Facades/ you must create the Settings.php file:

 <?php namespace Laracms\Facades; use Illuminate\Support\Facades\Facade; class Settings extends Facade { protected static function getFacadeAccessor() { return 'settings'; } } 

Now in your app/Laracms you should create the Settings.php file:

 <?php namespace Laracms; class Settings { public function get() {echo "get"; } public function set() {echo "set"; } } 

Since you wanted to have your files in the Laracms user folder, you need to add this folder to your composer.json (if you used the standard app/models folder, you would not need to add anything to this file). So now open the composer.json file and in the autoload โ†’ classmap you should add app/Laracms so that this composer.json section looks like this:

 "autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php", "app/Laracms" ] }, 

Now you need to start the console inside your project:

 composer dump-autoload 

to create a class map

If everything is in order, now you can use Settings::get() and Settings:set() in your applications

You should notice that I used folders with upper strings because namespaces by convention begin with uppercase letters.

+13
source

There are three components for creating a facade:

  • You want to become a Facade class, which should become a facade.
  • The Facade requires a class that tells Laravel who registered the class that it belongs to
  • A service provider that registers a Facade class in an App container

1. wish to be a facade class:

 <?php namespace Moubarmij\Services\ModelsServices; class AuthenticationService extends MoubarmijService implements AuthenticationServiceInterface{ /** * @param $email * @param $password * * @return mixed */ public function login($email, $password) { return Sentry::authenticate([ 'email' => $email, 'password' => $password, ]); } /** * @return mixed */ public function logout() { return Sentry::logout(); } } 

2. The required class for the operation of the facade:

 <?php namespace Moubarmij\Facades; use Illuminate\Support\Facades\Facade; /** * Class AuthenticationServiceFacade * @package Moubarmij\Services\ModelsServices */ class AuthenticationServiceFacade extends Facade{ /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'authentication_service'; } } 

note: authentication_service can be whatever you want (its component name is registered in IOC)

3. service provider

 <?php namespace Moubarmij\Providers; use Illuminate\Support\ServiceProvider; /** * A service provider for the Authentication Service * * Class AuthenticationServiceSP * @package Moubarmij\Providers */ class AuthenticationServiceSP extends ServiceProvider { /** * bind interfaces * * @return void */ public function register() { // Register 'authentication_service' instance container to our AuthenticationService object $this->app['authentication_service'] = $this->app->share(function($app) { return $app->make('Moubarmij\Services\ModelsServices\AuthenticationService'); }); // Shortcut to auto add the Alias in app/config/app.php $this->app->booting(function() { $loader = \Illuminate\Foundation\AliasLoader::getInstance(); $loader->alias('AuthenticationService', 'Moubarmij\Facades\AuthenticationServiceFacade'); }); } } 
+2
source

Source: https://habr.com/ru/post/975665/


All Articles