Home > Article > Backend Development > Laravel implements Facades function, laravelfacades_PHP tutorial
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> }
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> }
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>
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> }
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> ),
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> ),
Next, you can use this functional class in the project in the form of TestClass::do().