该外墙如何工作

发布于 2025-01-27 04:22:47 字数 1219 浏览 3 评论 0 原文

我正在与Laravel 5.8合作,这是其他程序员编写的在线商店项目。

基本上,我面临着从未见过的怪异的东西。

假设我们在控制器方法中有这个:

$payment = CourseRegistrationFacade::insertCourseRegisterInformation($courseId,$date,$memberId,$userId);

然后我们得到了 courseregistrationfacade ,它是这样的:

class CourseRegistrationFacade extends BaseFacade
{
    
}

所以整个班级都是空的,但它扩展了另一个<代码> BaseFacade ,它是这样的:

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    protected static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class)
    }
}

就是这样!

我真的不知道治愈在哪里 insertCoursereGisterInformation

因此,如果您知道该立面是如何工作的,请让我知道...


这是 basefacade.php 的完整代码:

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    public static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class);
    }
}

I'm working with Laravel 5.8 and it's an Online Store project written by other programmers.

Basically I have faced something weird that never seen before.

Let's say we have this at a Controller method:

$payment = CourseRegistrationFacade::insertCourseRegisterInformation($courseId,$date,$memberId,$userId);

And then we goto the CourseRegistrationFacade and it goes like this:

class CourseRegistrationFacade extends BaseFacade
{
    
}

So the whole class is empty but it extends another Facade which is BaseFacade and it goes like this:

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    protected static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class)
    }
}

And that's it !

I don't really know where the heal is insertCourseRegisterInformation !!

So if you know how this Facade works, please let me know...


Here is the full code of BaseFacade.php:

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    public static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2025-02-03 04:22:47

在代码中搜索:

CourseRegistrationFacade::shouldProxyTo(

最有可能在服务提供商中搜索该行在某个地方注册该类别的柜台的某个地方。然后检查类的内容(参数传递给了应该二氧基)。

在该类中,应该有一种称为InsertCourSereGisterInformation的方法。

立面的工作方式是他们从容器中解析了班级,然后呼叫您静态调用的方法。

因此,例如,假设您有一个带有方法寄存器()的userService.php,并且该类映射到userervicefacade.php。当您执行UserviceFacade :: register()时,__ callstatic将从容器中获取立面登录器(实际类),然后调用该类的寄存器()方法。

您可以通过检查__ callstatatic Inside.php来更好地理解。

从本质上讲,UserserviceFacade :: register()与做法相同:

$userService = app()->make(UserService::class);

$userService->register()

通过使用外墙,您可以隐藏具体的实现,并可能通过单个位置更改将来将其切换到其他东西。

Search in the code for:

CourseRegistrationFacade::shouldProxyTo(

Most likely in the service provider that line is somewhere registering that facade to some concrete implementation of a class. Then check the contents of the class (the argument passed to shouldProxyTo).

Inside that class there should be a method called insertCourseRegisterInformation.

The way facades work is they resolve the class out of the container and then call the method you call statically.

So for example, let's say you have a UserService.php with a method register() and that class is mapped to a UserServiceFacade.php. When you do UserServiceFacade::register(), __callStatic will get the facade accessor (actual class) from the container, then call the register() method of that class.

You can understand better by inspecting __callStatic inside Facade.php.

Essentially UserServiceFacade::register() is the same as doing:

$userService = app()->make(UserService::class);

$userService->register()

By using the facade you can hide the concrete implementation and could possibly switch it to something else in the future by just changing it in a single place.

迷爱 2025-02-03 04:22:47

我认为必须有一个提供商,该 facade 必须初始化其关联类, intertCourSereGisterInformation 方法>必须在其中声明。请找到该提供商,然后您会从该提供商代码找到其关联的类。我认为您可以从 config/app.php 找到所有注册的提供商,

这些参考文章可能会对您有所帮助。

参考文献1:

参考2: http://www.expertphp.in/article/how-to-to-to-to-create-custom-custom-facade-in-laravel-in-laravel-52

I think there must be a Provider exists for that Facade which is initializing its associated class and insertCourseRegisterInformation method definition must be declared in it. Please find that provider and then you'll find its associated class from that Provider code. I think you can find all registered providers from config/app.php

These reference articles might help you.

Reference 1:
https://grafxflow.co.uk/blog/mvc/adding-global-custom-class-facades-laravel-5

Reference 2: http://www.expertphp.in/article/how-to-create-custom-facade-in-laravel-52

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文