Contracts
Contracts其實就是倡導面向介面編程,來達到解耦的目的。而這些通用的介面已經由Laravel為你設計好了。就是這些Contracts.
那麼Laravel如何知道我們需要使用哪個實作呢?
在Laravel預設的Contracts綁定中,在'Illuminate/Foundation/Application.php'有這樣的定義:這就是綁定了預設的介面實作.
#推薦:《laravel教學》
/** * Register the core class aliases in the container. * * @return void */ public function registerCoreContainerAliases() { $aliases = [ 'app' => ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container', 'Illuminate\Contracts\Foundation\Application'], 'auth' => 'Illuminate\Auth\AuthManager', 'auth.driver' => ['Illuminate\Auth\Guard', 'Illuminate\Contracts\Auth\Guard'], 'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface', 'blade.compiler' => 'Illuminate\View\Compilers\BladeCompiler', 'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'], 'cache.store' => ['Illuminate\Cache\Repository', 'Illuminate\Contracts\Cache\Repository'], 'config' => ['Illuminate\Config\Repository', 'Illuminate\Contracts\Config\Repository'], 'cookie' => ['Illuminate\Cookie\CookieJar', 'Illuminate\Contracts\Cookie\Factory', 'Illuminate\Contracts\Cookie\QueueingFactory'], 'encrypter' => ['Illuminate\Encryption\Encrypter', 'Illuminate\Contracts\Encryption\Encrypter'], 'db' => 'Illuminate\Database\DatabaseManager', 'db.connection' => ['Illuminate\Database\Connection', 'Illuminate\Database\ConnectionInterface'], 'events' => ['Illuminate\Events\Dispatcher', 'Illuminate\Contracts\Events\Dispatcher'], 'files' => 'Illuminate\Filesystem\Filesystem', 'filesystem' => ['Illuminate\Filesystem\FilesystemManager', 'Illuminate\Contracts\Filesystem\Factory'], 'filesystem.disk' => 'Illuminate\Contracts\Filesystem\Filesystem', 'filesystem.cloud' => 'Illuminate\Contracts\Filesystem\Cloud', 'hash' => 'Illuminate\Contracts\Hashing\Hasher', 'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'], 'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'], 'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'], 'auth.password' => ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'], 'queue' => ['Illuminate\Queue\QueueManager', 'Illuminate\Contracts\Queue\Factory', 'Illuminate\Contracts\Queue\Monitor'], 'queue.connection' => 'Illuminate\Contracts\Queue\Queue', 'redirect' => 'Illuminate\Routing\Redirector', 'redis' => ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'], 'request' => 'Illuminate\Http\Request', 'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar'], 'session' => 'Illuminate\Session\SessionManager', 'session.store' => ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'], 'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'], 'validator' => ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'], 'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'], ];
在我們自訂的介面實作時,我們可以在ServiceProvider中使用進行綁定:
$this->app->bind('App\Contracts\EventPusher', 'App\Services\PusherEventPusher');
Facades
Facades 為應用程式的服務容器中可用的類別提供了一個「靜態」介面。 Laravel “facades”作為在服務容器內基類的「靜態代理」。很難懂?
我們開啟專案目錄下的config/app.php,然後找到
/* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, 'Blade' => Illuminate\Support\Facades\Blade::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, 'Config' => Illuminate\Support\Facades\Config::class, 'Cookie' => Illuminate\Support\Facades\Cookie::class, 'Crypt' => Illuminate\Support\Facades\Crypt::class, 'DB' => Illuminate\Support\Facades\DB::class, 'Eloquent' => Illuminate\Database\Eloquent\Model::class, 'Event' => Illuminate\Support\Facades\Event::class, 'File' => Illuminate\Support\Facades\File::class, 'Gate' => Illuminate\Support\Facades\Gate::class, 'Hash' => Illuminate\Support\Facades\Hash::class, 'Input' => Illuminate\Support\Facades\Input::class, 'Lang' => Illuminate\Support\Facades\Lang::class, 'Log' => Illuminate\Support\Facades\Log::class, 'Mail' => Illuminate\Support\Facades\Mail::class, 'Password' => Illuminate\Support\Facades\Password::class, 'Queue' => Illuminate\Support\Facades\Queue::class, 'Redirect' => Illuminate\Support\Facades\Redirect::class, 'Redis' => Illuminate\Support\Facades\Redis::class, 'Request' => Illuminate\Support\Facades\Request::class, 'Response' => Illuminate\Support\Facades\Response::class, 'Route' => Illuminate\Support\Facades\Route::class, 'Schema' => Illuminate\Support\Facades\Schema::class, 'Session' => Illuminate\Support\Facades\Session::class, 'Storage' => Illuminate\Support\Facades\Storage::class, 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, ],
你是不是發現了什麼?對,Facades其實就是在config/app.php中定義的一系列類別的別名。只不過這些類別都有一個共同的特點,那就是繼承基底 Illuminate\Support\Facades\Facade 類別並實作一個方法:getFacadeAccessor傳回名稱。
自訂Facade
參考http://www.tutorialspoint.com/laravel/laravel_facades.htm
Step 1 −建立一個名為TestFacadesServiceProvider的ServiceProvider ,使用以下命令即可:
php artisan make:provider TestFacadesServiceProvider
#Step 2 − 建立一個底層代理類,命名為「TestFacades.php」 at “App/Test”.
App/Test/TestFacades.php
<?php namespace App\Test; class TestFacades{ public function testingFacades(){ echo "Testing the Facades in Laravel."; } } ?>
Step 3 − 建立一個Facade 類別called “TestFacades. php” at “App/Test/Facades”.
App/Test/Facades/TestFacades.php
<?php namespace app\Test\Facades; use Illuminate\Support\Facades\Facade; class TestFacades extends Facade{ protected static function getFacadeAccessor() { return 'test'; } }
Step 4 −建立一個ServiceProviders類,名稱為「 TestFacadesServiceProviders.php」 at “App/Test/Facades”.
App/Providers/TestFacadesServiceProviders.php
<?php namespace App\Providers; use App; use Illuminate\Support\ServiceProvider; class TestFacadesServiceProvider extends ServiceProvider { public function boot() { // } public function register() { //可以这么绑定,这需要use App; // App::bind('test',function() { // return new \App\Test\TestFacades; // }); //也可以这么绑定,推荐。这个test对应于Facade的getFacadeAccessor返回值 $this->app->bind("test", function(){ return new MyFoo(); //给这个Facade返回一个代理实例。所有对Facade的调用都会被转发到该类对象下。 }); } }
Step 5 − 在config/app.phpServiceProviderer類別
Step 6 − 在config/app.php註冊自訂Facade的別名
使用測試:
Add the following lines in app/ Http/routes.php.
Route::get('/facadeex', function(){ return TestFacades::testingFacades(); });
Step 9 − Visit the following URL to test the Facade.
http://localhost:8000/facadeex去查看輸出
以上是Laravel之Contracts和Facades詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

選擇Laravel或Python取決於項目需求:1)若需快速開發Web應用並使用ORM和認證系統,選Laravel;2)若涉及數據分析、機器學習或科學計算,選Python。

Laravel适合快速构建Web应用,Python适用于需要灵活性和多功能性的项目。1)Laravel提供丰富功能如ORM和路由,适合PHP生态系统。2)Python以简洁语法和强大库生态系统著称,适用于Web开发和数据科学等领域。

使用Laravel和PHP可以高效且有趣地創建動態網站。 1)Laravel遵循MVC架構,Blade模板引擎簡化HTML編寫。 2)路由系統和請求處理機制使URL定義和用戶輸入處理變得簡單。 3)EloquentORM簡化數據庫操作。 4)通過博客系統示例展示了數據庫遷移、CRUD操作和Blade模板的使用。 5)Laravel提供了強大的用戶認證和授權功能。 6)調試技巧包括使用日誌系統和Artisan工具。 7)性能優化建議包括惰性加載和緩存。

Laravel通過Blade模板引擎、EloquentORM、Artisan工具和LaravelMix實現全棧開發:1.Blade簡化前端開發;2.Eloquent簡化數據庫操作;3.Artisan提高開發效率;4.LaravelMix管理前端資源。

Laravel是一個基於PHP的現代化框架,遵循MVC架構模式,提供了豐富的工具和功能,簡化了Web開發過程。 1)它包含EloquentORM用於數據庫交互,2)Artisan命令行接口用於快速生成代碼,3)Blade模板引擎用於高效的視圖開發,4)強大的路由系統用於定義URL結構,5)認證系統用於用戶管理,6)事件監聽和廣播用於實時功能,7)緩存和隊列系統用於性能優化,使得構建和維護現代Web應用變得更加容易和高效。

Laravel适合快速构建Web应用,而Python适用于更广泛的应用场景。1.Laravel提供EloquentORM、Blade模板引擎和Artisan工具,简化Web开发。2.Python以动态类型、丰富的标准库和第三方生态系统著称,适用于Web开发、数据科学等领域。

Laravel和Python各有優勢:Laravel適合快速構建功能豐富的Web應用,Python在數據科學和通用編程領域表現出色。 1.Laravel提供EloquentORM和Blade模板引擎,適合構建現代Web應用。 2.Python擁有豐富的標準庫和第三方庫,Django和Flask框架滿足不同開發需求。

Laravel值得選擇,因為它能使代碼結構清晰,開發過程更具藝術性。 1)Laravel基於PHP,遵循MVC架構,簡化Web開發。 2)其核心功能如EloquentORM、Artisan工具和Blade模板增強了開發的優雅與健壯性。 3)通過路由、控制器、模型和視圖,開發者能高效構建應用。 4)隊列和事件監聽等高級功能進一步提升應用性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中