繁体   English   中英

如何在Laravel 4中创建自定义Facade

[英]How to create custom Facade in Laravel 4

看了几个关于外墙和laravel 4的教程...尝试了一些......不喜欢他们工作的方式。

例如,他们并不都提供了一种定义外观文件和服务提供商存储位置的方法......我试图远离那个并让我的头撞到几个墙,直到我决定做这个线程。

所以:假设我有一个名为Laracms(laravel cms)的应用程序。

我想将我创建的所有内容 - 外墙,服务提供商等存储在名为laracms的app下的文件夹中。

所以我有/ app / laracms / facades,/ app / laracms / serviceproviders等等。 我不想将外观与数据库模型混合在一起,我希望尽可能保持分离。

在我的例子中,现在让我们看一下外观的设置名称(我希望实现一个设置类,用于视图和管理员来设置misc。的东西)。

Settings :: get(),Settings :: set()作为方法。

任何人都可以解释如何正确设置外墙? 我不知道我做错了什么,我需要一个新的开始。

谢谢,克里斯

寻找一步一步的简单解释如何以及为什么。

首先你需要去app/config/app.php并在providers部分添加:

'Laracms\Providers\SettingsServiceProvider',

aliases部分的同一文件中,您应该添加:

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

在您的app/Laracms/Providers您应该创建文件SettingsServiceProvider.php

<?php

namespace Laracms\Providers;

use Illuminate\Support\ServiceProvider;

class SettingsServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('settings', function()
            {
                return new \Laracms\Settings();
            });
    }

}

在您的app/Laracms/Facades/您应该创建文件Settings.php

<?php

namespace Laracms\Facades;

use Illuminate\Support\Facades\Facade;

class Settings extends Facade {

    protected static function getFacadeAccessor() { return 'settings'; }

}

现在,在app/Laracms目录中,您应该创建文件Settings.php

<?php

namespace Laracms;

class Settings {
   public function get() {echo "get"; }

   public function set() {echo "set"; }
}

由于您希望将文件放在自定义文件夹Laracms您需要将此文件夹添加到composer.json (如果您使用标准app/models文件夹,则无需向此文件添加任何内容)。 所以现在打开composer.json文件,在autoload - > classmap你应该添加app/Laracms这样composer.json的这一部分可能如下所示:

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

现在,您需要在项目内部的控制台中运行:

composer dump-autoload

创建类映射

如果一切正常,您现在应该能够在您的应用程序中使用Settings::get()Settings:set()

您需要注意我使用了带有大写字母的文件夹,因为按照惯例,命名空间以大写字母开头。

制作Facade有三个组件:

  • 想成为门面类,那个需要成为立面的类。
  • Facade需要Class,它告诉Laravel它所属的注册类
  • 服务提供程序,它在App容器中注册Facade类

1.想成为立面课:

<?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.立面工作所需的类:

<?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'; }


}

注意:authentication_service可以是你想要的任何东西(它是在IOC中注册的组件的名称)

3.服务提供商

<?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');
        });

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM