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()的形式使用该功能类了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Database operations in PHP are simplified using ORM, which maps objects into relational databases. EloquentORM in Laravel allows you to interact with the database using object-oriented syntax. You can use ORM by defining model classes, using Eloquent methods, or building a blog system in practice.

PHP unit testing tool analysis: PHPUnit: suitable for large projects, provides comprehensive functionality and is easy to install, but may be verbose and slow. PHPUnitWrapper: suitable for small projects, easy to use, optimized for Lumen/Laravel, but has limited functionality, does not provide code coverage analysis, and has limited community support.

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original

PHP Unit and Integration Testing Guide Unit Testing: Focus on a single unit of code or function and use PHPUnit to create test case classes for verification. Integration testing: Pay attention to how multiple code units work together, and use PHPUnit's setUp() and tearDown() methods to set up and clean up the test environment. Practical case: Use PHPUnit to perform unit and integration testing in Laravel applications, including creating databases, starting servers, and writing test code.

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.
