Laravel implements Facades function, laravelfacades_PHP tutorial

WBOY
Release: 2016-07-13 10:09:42
Original
867 people have browsed it

Laravel implements the Facades function, laravelfacades

  Students who have used Laravel know the power of Facades. Let's create a Facades instance together. If there is anything incorrect, please feel free to enlighten me.

1. Implement Laravel’s automatic loading function

First create the directory app/lib/Myapp, and then add the directory to composer.json

<span>1</span> "autoload":<span> {        
</span><span>2</span>     "psr-0":<span>{
</span><span>3</span>         "Myapp":"app/lib"
<span>4</span> <span>    }
</span><span>5</span> }    
Copy after login

 

2. Implement functional classes

Implementation class app/lib/Myapp/Test.php 

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

  3. Implement ServiceProvider

Implement app/lib/Myapp/TestServiceProvider.php. This class mainly adds functional classes to the Ioc container:

<?<span>php
</span><span>/*</span><span>*
 * @author brudeke
 </span><span>*/</span><span>
namespace Myapp;

</span><span>use</span><span> Illuminate\Support\ServiceProvider;

</span><span>class</span> TestServiceProvider <span>extends</span><span> ServiceProvider{

    </span><span>public</span> <span>function</span><span> register()  {
        </span><span>$this</span>->app['test'] = <span>$this</span>->app-><span>share(
            </span><span>function</span> (<span>$app</span><span>) {
                </span><span>return</span> <span>new</span><span> \Myapp\Test();
        });
    }
}</span>
Copy after login

 4. Implement Facade instance

Implement app/lib/Myapp/Facades/TestFacades.php, the main beautification of this class, and realize the calling form of member functions similar to static methods

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * @author brudeke
</span><span> 4</span>  <span>*/</span>
<span> 5</span> <span>namespace Myapp\Facades;
</span><span> 6</span> 
<span> 7</span> <span>use</span><span> Illuminate\Support\Facades\Facade;
</span><span> 8</span> 
<span> 9</span> <span>class</span>  TestFacades <span>extends</span><span> Facade{
</span><span>10</span>     <span>protected</span> <span>static</span> <span>function</span><span> getFacadeAccessor()
</span><span>11</span> <span>    {
</span><span>12</span>         <span>return</span> 'test'<span>;
</span><span>13</span> <span>    }
</span><span>14</span> }
Copy after login

 5. Load ServiceProvider

Add the following configuration to providers in app/config/app.php:

<span>1</span> 'providers' => <span>array</span><span>(   
</span><span>2</span>   'Myapp\TestServiceProvider'
<span>3</span> ),
Copy after login

Add aliases in aliases in app/config/app.php:

<span>1</span> 'aliases' => <span>array</span><span>(
</span><span>2</span>     'TestClass'            =>  'Myapp\Facades\TestFacades',
<span>3</span> ),
Copy after login

Next, you can use this functional class in the project in the form of TestClass::do().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/944439.htmlTechArticleLaravel implements the Facades function, laravelfacades. Students who have used Laravel know the power of Facades. Let's create one together. Facades instance. If there are any inaccuracies, please also...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!