search
Homephp教程php手册Laravel 实现 Facades 功能,laravelfacades

Laravel 实现 Facades 功能,laravelfacades

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

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

  首先建立目录app/lib/Myapp,然后添加目录到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. 实现功能类

  实现能能类 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. 实现ServiceProvider

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

<?<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. 实现Facade 实例

  实现app/lib/Myapp/Facades/TestFacades.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\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. 加载ServiceProvider

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

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

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

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

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.