> 백엔드 개발 > PHP 튜토리얼 > Laravel 实现 Facades 效能

Laravel 实现 Facades 效能

WBOY
풀어 주다: 2016-06-13 12:13:45
원래의
1113명이 탐색했습니다.

Laravel 实现 Facades 功能

  使用过Laravel的同学都知道Facades 的强大,下面就让我们一起创建一个Facades 实例。如有不正确的地方,还请不吝赐教。

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

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

 

<span style="color: #008080;">1</span> "autoload":<span style="color: #000000;"> {        </span><span style="color: #008080;">2</span>     "psr-0":<span style="color: #000000;">{</span><span style="color: #008080;">3</span>         "Myapp":"app/lib"<span style="color: #008080;">4</span> <span style="color: #000000;">    }</span><span style="color: #008080;">5</span> }    
로그인 후 복사

  

  2. 实现功能类

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

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * @author brudeke</span><span style="color: #008080;"> 4</span>  <span style="color: #008000;">*/</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">namespace Myapp;</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{</span><span style="color: #008080;"> 7</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> <span style="color: #0000ff;">do</span><span style="color: #000000;">(){</span><span style="color: #008080;"> 8</span>         <span style="color: #0000ff;">echo</span> 'this is a class'<span style="color: #000000;">;</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    }</span><span style="color: #008080;">10</span> }
로그인 후 복사

  

  3. 实现ServiceProvider

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

<span style="color: #000000;">php</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @author brudeke </span><span style="color: #008000;">*/</span><span style="color: #000000;">namespace Myapp;</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Illuminate\Support\ServiceProvider;</span><span style="color: #0000ff;">class</span> TestServiceProvider <span style="color: #0000ff;">extends</span><span style="color: #000000;"> ServiceProvider{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register()  {        </span><span style="color: #800080;">$this</span>->app['test'] = <span style="color: #800080;">$this</span>->app-><span style="color: #000000;">share(            </span><span style="color: #0000ff;">function</span> (<span style="color: #800080;">$app</span><span style="color: #000000;">) {                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> \Myapp\Test();        });    }}</span>
로그인 후 복사

 

  4. 实现Facade 实例

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

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * @author brudeke</span><span style="color: #008080;"> 4</span>  <span style="color: #008000;">*/</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">namespace Myapp\Facades;</span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">use</span><span style="color: #000000;"> Illuminate\Support\Facades\Facade;</span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">class</span>  TestFacades <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Facade{</span><span style="color: #008080;">10</span>     <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getFacadeAccessor()</span><span style="color: #008080;">11</span> <span style="color: #000000;">    {</span><span style="color: #008080;">12</span>         <span style="color: #0000ff;">return</span> 'test'<span style="color: #000000;">;</span><span style="color: #008080;">13</span> <span style="color: #000000;">    }</span><span style="color: #008080;">14</span> }
로그인 후 복사

  

  5. 加载ServiceProvider

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

<span style="color: #008080;">1</span> 'providers' => <span style="color: #0000ff;">array</span><span style="color: #000000;">(   </span><span style="color: #008080;">2</span>   'Myapp\TestServiceProvider'<span style="color: #008080;">3</span> ),
로그인 후 복사

  在app/config/app.php 中的aliases中添加别名:  

<span style="color: #008080;">1</span> 'aliases' => <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">2</span>     'TestClass'            =>  'Myapp\Facades\TestFacades',<span style="color: #008080;">3</span> ),
로그인 후 복사

  接下来就可以在项目中以TestClass::do()的形式使用该功能类了。

1楼brudeke
11111
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿