Php 为什么拉威尔';她的外表表现得像个单身汉?

Php 为什么拉威尔';她的外表表现得像个单身汉?,php,laravel,Php,Laravel,我知道拉威尔用的是立面。但基本上我无法理解他们为什么要用它。让我们看看可能文件外观类。既然已经有了文件系统类,为什么还要将文件类作为facade呢?我们不能在我们的代码中写这样的东西吗 $file = new \Illuminate\FileSystem(); $path = $file->get(public_path("test.txt")); 但laravel决定使用File facade类,之后我可以编写如下内容: $path = File::get(public_path("t

我知道拉威尔用的是立面。但基本上我无法理解他们为什么要用它。让我们看看可能文件外观类。既然已经有了文件系统类,为什么还要将文件类作为facade呢?我们不能在我们的代码中写这样的东西吗

$file = new \Illuminate\FileSystem();
$path = $file->get(public_path("test.txt"));
但laravel决定使用File facade类,之后我可以编写如下内容:

$path = File::get(public_path("test.txt")); 
美在哪里?若你们看一下facade模式,它为你们提供了一个更容易的界面来完成大的工作,并且当你们可以用两行代码来完成facade时,不需要使用10行代码,但laravel的facade让你们很难理解

这可能是因为他们制作了facade类,使它像一个单体一样工作,这是它惊人的一面吗?但是为什么单身汉令人惊奇呢


我不明白拉雷维尔的正面是什么意思

Laravel具有类似于立面设计模式的功能,也称为立面。这个名称可能会让您感到困惑,因为Laravel中的facades没有完全实现Facade设计模式。根据

Facades为应用程序服务容器中可用的类提供“静态”接口

因此,Facade将允许我们使用接口,而不必担心这些接口背后的实际实现。 让我们以Laravel缓存系统为例。 当我们调用
$items=Cache::get('items:popular')

在这里,我们借助cachefacade从缓存中检索项目

所有门面类都是从基本门面类扩展而来的。只有一个方法必须在每个facade类中实现:getFacadeAccessor(),它返回IoC容器中唯一的服务名称。所以它必须返回一个字符串,然后从IoC容器中解析出来

以下是照明\Support\Facades\Cache门面类的源代码:

<?php 
namespace Illuminate\Support\Facade;
class Cache extends Facade 
{
    protected static function getFacadeAccessor()
    {
        return 'cache';
    }
}
方法
getFacadeRoot()
返回facade后面的服务对象的实例。在本例中,它最终指向CacheManager类。在CacheManager类中,我们有一个
getDefaultDriver()
方法,该方法将从
.env
文件中获取默认缓存配置

public function getDefaultDriver()
{
    return $this->app['config']['cache.default'];
}
获取默认缓存配置后,使用PHP magic方法
\u call()
尝试调用默认缓存(redis、数据库、memcached等)的具体类上的
get()
方法

所以我们最初调用的
$items=Cache::get('items:popular')不会更改。大多数人使用数据库作为开发的缓存,使用redismemcached作为后端。Laravel Facades的任务是找出要执行哪些操作才能从缓存中获取值。例如,redis的
get()
实现是

public function get($key)
{
    $value = $this->connection()->get($this->prefix.$key);

    return ! is_null($value) ? $this->unserialize($value) : null;
}
public function get($key)
{
    $value = $this->memcached->get($this->prefix.$key);

    if ($this->memcached->getResultCode() == 0) {
        return $value;
    }
}
get()

public function get($key)
{
    $value = $this->connection()->get($this->prefix.$key);

    return ! is_null($value) ? $this->unserialize($value) : null;
}
public function get($key)
{
    $value = $this->memcached->get($this->prefix.$key);

    if ($this->memcached->getResultCode() == 0) {
        return $value;
    }
}

类似地,您可以缓存另一个具体类Laravel将为您决定调用哪个具体实现。

Laravel具有类似于Facade设计模式的功能,也称为Facades。这个名称可能会让您感到困惑,因为Laravel中的facades没有完全实现Facade设计模式。根据

Facades为应用程序服务容器中可用的类提供“静态”接口

因此,Facade将允许我们使用接口,而不必担心这些接口背后的实际实现。 让我们以Laravel缓存系统为例。 当我们调用
$items=Cache::get('items:popular')

在这里,我们借助cachefacade从缓存中检索项目

所有门面类都是从基本门面类扩展而来的。只有一个方法必须在每个facade类中实现:getFacadeAccessor(),它返回IoC容器中唯一的服务名称。所以它必须返回一个字符串,然后从IoC容器中解析出来

以下是照明\Support\Facades\Cache门面类的源代码:

<?php 
namespace Illuminate\Support\Facade;
class Cache extends Facade 
{
    protected static function getFacadeAccessor()
    {
        return 'cache';
    }
}
方法
getFacadeRoot()
返回facade后面的服务对象的实例。在本例中,它最终指向CacheManager类。在CacheManager类中,我们有一个
getDefaultDriver()
方法,该方法将从
.env
文件中获取默认缓存配置

public function getDefaultDriver()
{
    return $this->app['config']['cache.default'];
}
获取默认缓存配置后,使用PHP magic方法
\u call()
尝试调用默认缓存(redis、数据库、memcached等)的具体类上的
get()
方法

所以我们最初调用的
$items=Cache::get('items:popular')不会更改。大多数人使用数据库作为开发的缓存,使用redismemcached作为后端。Laravel Facades的任务是找出要执行哪些操作才能从缓存中获取值。例如,redis的
get()
实现是

public function get($key)
{
    $value = $this->connection()->get($this->prefix.$key);

    return ! is_null($value) ? $this->unserialize($value) : null;
}
public function get($key)
{
    $value = $this->memcached->get($this->prefix.$key);

    if ($this->memcached->getResultCode() == 0) {
        return $value;
    }
}
get()

public function get($key)
{
    $value = $this->connection()->get($this->prefix.$key);

    return ! is_null($value) ? $this->unserialize($value) : null;
}
public function get($key)
{
    $value = $this->memcached->get($this->prefix.$key);

    if ($this->memcached->getResultCode() == 0) {
        return $value;
    }
}

类似地,您可以缓存另一个具体类Laravel将为您决定调用哪个具体实现。

这是为了使某些功能可交换,例如存储可以是文件、AWS、dropbox等。这是为了使某些功能可交换,例如存储可以是文件、AWS、dropbox等。