نماها
معرفی
نماها یک رابط "ایستا" را برای کلاس هایی که در کانتینر سرویس برنامه در دسترس هستند ارائه می کنند . کشتی های لاراول با نماهای بسیار زیاد، و شما احتمالا بدون اینکه بدانید از آنها استفاده کرده اید! "نماهای" لاراول به عنوان "پراکسی های استاتیک" برای کلاس های زیرین در کانتینر سرویس عمل می کنند و از یک نحو مختصر و رسا استفاده می کنند و در عین حال تست پذیری و انعطاف پذیری بیشتری نسبت به روش های استاتیک سنتی حفظ می کنند.
گاهی اوقات، ممکن است بخواهید نماهای خود را برای برنامه ها و بسته های خود ایجاد کنید، بنابراین بیایید مفهوم، توسعه و استفاده از این کلاس ها را بررسی کنیم.
توجه: قبل از کند و کاو در نما، اکیداً توصیه می شود که با کانتینر سرویس لاراول بسیار آشنا شوید .
توضیح
در زمینه برنامه لاراول، نما کلاسی است که دسترسی به یک شی را از کانتینر فراهم می کند.
ماشین آلاتی که این کار را می کند در
Facade
کلاس است.
نماهای لاراول و هر نمای سفارشی که ایجاد می کنید،
Facade
کلاس پایه را گسترش می دهد.
کلاس نما شما فقط نیاز به پیاده سازی یک روش دارد:
getFacadeAccessor
.
این
getFacadeAccessor
وظیفه روش است که مشخص کند چه چیزی را از ظرف حل کند.
کلاس پایه از
روش جادویی برای به تعویق انداختن تماس ها از نمای شما به شی حل شده
Facade
استفاده می کند .
__callStatic()
So, when you make a facade call like Cache::get
, Laravel resolves the Cache manager class out of the service container and calls the get
method on the class. In technical terms, Laravel Facades are a convenient syntax for using the Laravel service container as a service locator.
Practical Usage
In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method get
is being called on the Cache
class.
$value = Cache::get('key');
However, if we look at that Illuminate\Support\Facades\Cache
class, you'll see that there is no static method get
:
class Cache extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'cache'; } }
The Cache class extends the base Facade
class and defines a method getFacadeAccessor()
. Remember, this method's job is to return the name of a service container binding.
When a user references any static method on the Cache
facade, Laravel resolves the cache
binding from the service container and runs the requested method (in this case, get
) against that object.
So, our Cache::get
call could be re-written like so:
$value = $app->make('cache')->get('key');
Importing Facades
Remember, if you are using a facade in a controller that is namespaced, you will need to import the facade class into the namespace. All facades live in the global namespace:
<?php namespace App\Http\Controllers; use Cache; class PhotosController extends Controller { /** * Get all of the application photos. * * @return Response */ public function index() { $photos = Cache::get('photos'); // } }
Creating Facades
Creating a facade for your own application or package is simple. You only need 3 things:
- A service container binding.
- A facade class.
- A facade alias configuration.
Let's look at an example. Here, we have a class defined as PaymentGateway\Payment
.
namespace PaymentGateway; class Payment { public function process() { // } }
We need to be able to resolve this class from the service container. So, let's add a binding to a service provider:
App::bind('payment', function(){ return new \PaymentGateway\Payment;});
A great place to register this binding would be to create a new service provider named PaymentServiceProvider
, and add this binding to the register
method. You can then configure Laravel to load your service provider from the config/app.php
configuration file.
Next, we can create our own facade class:
use Illuminate\Support\Facades\Facade; class Payment extends Facade { protected static function getFacadeAccessor() { return 'payment'; } }
Finally, if we wish, we can add an alias for our facade to the aliases
array in the config/app.php
configuration file. Now, we can call the process
method on an instance of the Payment
class.
Payment::process();
A Note On Auto-Loading Aliases
Classes in the aliases
array are not available in some instances because PHP will not attempt to autoload undefined type-hinted classes. If \ServiceWrapper\ApiTimeoutException
is aliased to ApiTimeoutException
, a catch(ApiTimeoutException $e)
outside of the namespace \ServiceWrapper
will never catch the exception, even if one is thrown. A similar problem is found in classes which have type hints to aliased classes. The only workaround is to forego aliasing and use
the classes you wish to type hint at the top of each file which requires them.
Mocking Facades
تست واحد یکی از جنبه های مهم این است که چرا نماها به روشی که انجام می دهند کار می کنند. در واقع، آزمایش پذیری دلیل اصلی حتی وجود نماها است. برای اطلاعات بیشتر، بخش نماهای تقلید آمیز اسناد را بررسی کنید .
مرجع کلاس نما
در زیر هر نما و طبقه زیرین آن را خواهید یافت. این یک ابزار مفید برای کاوش سریع در اسناد API برای ریشه نما معین است. کلید صحافی کانتینر سرویس نیز در صورت لزوم گنجانده شده است.