一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Laravel 创建自定义 Facades 类的例子

时间:2022-06-25 00:42:55 编辑:袖梨 来源:一聚教程网

使用Laravel框架必不可少的会用到它很多强大的门面类(Facades),门面提供了一个“静态”接口到服务容器中绑定的类,官方文档阐述了如何使用系统自带的缓存门面,我们这里演示如何创建并使用一个自定义的门面类。

注:本教程基于上一节服务提供者做部分代码修改,不熟悉的请参阅。
我们首先创建一个需要绑定到服务容器的Test类:

namespace AppFacades;

class Test
{
public function doSomething()
{
echo 'This is TestClass's method doSomething';
}
}
然后创建一个静态指向Test类的门面类TestClass:

namespace AppFacades;

use IlluminateSupportFacadesFacade;

class TestClass extends Facade
{
protected static function getFacadeAccessor()
{
return 'test';
}
}

接下来我们要在服务提供者中绑定Test类到服务容器,修改TestServiceProvider类如下:

namespace AppProviders;

use IlluminateSupportServiceProvider;
use AppServicesTestService;
use AppFacadesTest;

class TestServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{

}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('test',function(){
//return new TestService();
return new Test;
});

$this->app->bind('AppContractsTestContract',function(){
return new TestService();
});
}
}

再然后需要到配置文件config/app.php中注册门面类别名:

'aliases' => [

...//其他门面类别名映射

'TestClass' => AppFacadesTestClass::class,
],

最后修改TestController代码如下:

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppHttpRequests;
use AppHttpControllersController;

use App;
use TestClass;
use AppContractsTestContract;

class TestController extends Controller
{

public function __construct(TestContract $test){
$this->test = $test;
}

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
// $test = App::make('test');
// $test->callMe('TestController');
//$this->test->callMe('TestController');

TestClass::doSomething();
}

...//其他方法
}

注意:不要忘了在调用门面类TestClass的文件顶部使用use TestClass;引入TestClass,否则将不能正确调用。
好了,我们可以去浏览器中测试了,访问http://la**ravel*.app:8000/test,页面将会输出:

This is TestClass's method doSomething

实现 Facades 功能另一篇文章

1. 实现Laravel的自动加载功能

首先建立目录app/lib/Myapp,然后添加目录到composer.json中

1 "autoload": {
2 "psr-0":{
3 "Myapp":"app/lib"
4 }
5 }

2. 实现功能类

实现能能类 app/lib/Myapp/Test.php

1 2 /**
3 * @author brudeke
4 */
5 namespace Myapp;
6 class Test{
7 public function do(){
8 echo 'this is a class';
9 }
10 }

3. 实现ServiceProvider

实现app/lib/Myapp/TestServiceProvider.php ,该类主要是将功能类添加到Ioc容器:

/**
* @author brudeke
*/
namespace Myapp;

use IlluminateSupportServiceProvider;

class TestServiceProvider extends ServiceProvider{

public function register() {
$this->app['test'] = $this->app->share(
function ($app) {
return new MyappTest();
});
}
}

4. 实现Facade 实例

实现app/lib/Myapp/Facades/TestFacades.php, 该类的主要美化,实现成员函数类似静态方法的调用形式

1 2 /**
3 * @author brudeke
4 */
5 namespace MyappFacades;
6
7 use IlluminateSupportFacadesFacade;
8
9 class TestFacades extends Facade{
10 protected static function getFacadeAccessor()
11 {
12 return 'test';
13 }
14 }

5. 加载ServiceProvider

在app/config/app.php 中的providers中添加如下配置:

1 'providers' => array(
2   'MyappTestServiceProvider'
3 ),
在app/config/app.php 中的aliases中添加别名:

1 'aliases' => array(
2 'TestClass' => 'MyappFacadesTestFacades',
3 ),
接下来就可以在项目中以TestClass::do()的形式使用该功能类了

热门栏目