Php Laravel:无法访问Facade的受保护功能

Php Laravel:无法访问Facade的受保护功能,php,laravel,Php,Laravel,我想通过扩展Facades\Auth重写SessionGuard的onceBasic(),但遇到无法访问SessionGuard类的受帮助程序保护的函数的问题。 以下是我扩展Auth facades的方法: namespace App\MyFacades; use \Illuminate\Support\Facades\Auth; class Auth_QuyLe extends Auth { /** * Create your custom methods

我想通过扩展
Facades\Auth
重写
SessionGuard
onceBasic()
,但遇到无法访问
SessionGuard
类的受帮助程序保护的函数的问题。 以下是我扩展Auth facades的方法:

namespace App\MyFacades;
use \Illuminate\Support\Facades\Auth;

class Auth_QuyLe extends Auth {

        /**
         * Create your custom methods here...
         */
        public static function onceBasic_QuyLe($field = 'name', $extraConditions = [])
        {   
            $credentials =  self::basicCredentials(self::getRequest(), $field);

            if (! self::once(array_merge($credentials, $extraConditions))) {
                return self::failedBasicResponse();
            }
        }

}
当我从中间件调用
onceBasic_QuyLe()
时,它显示

"Method Illuminate\Auth\SessionGuard::basicCredentials does not exist."

我已经将我的类更新为config/app.php并运行
composer dump autoload&&php artisan config:cache

我终于找到了一个解决方案:

首先,b/c
SessionGuard
是一个“可宏”类,如果需要向
SessionGuard
添加其他方法,则需要转到
AppServiceProvider
并将宏声明到服务提供商的引导方法中

public function boot()
    {
        SessionGuard::macro('myOnceBasic', function ($field = 'name', $extraConditions = []) {

            $credentials = $this->basicCredentials($this->getRequest(), $field);

            if (!$this->once(array_merge($credentials, $extraConditions))) {
                return$this->failedBasicResponse();
            }
        });
}
在新创建的Facades表示的基础类中,可以使用Auth facade调用宏:

class MySessionGuard{

        public function myOnceBasic($field = 'name', $extraConditions = [])
        {   
            Auth::myOnceBasic($field = 'name', $extraConditions = []);
        }

}

不要试图对外观进行任何更改。Facades只是解析基础类的一个实例,在本例中,该实例是当前使用的Auth guard(SessionGuard)。。。我不改变外表。。。我不会修改laraval core的任何代码。。。我所做的是“扩展它”,创建我自己的功能,利用来自AuthFacades的一些助手。。。这里有一些类似的帖子在SO:| |我不知道为什么在我的情况下,我就是无法访问Auth facades的受保护功能,尽管公共功能很好……对不起,我不是想暗示你在编辑核心内容。我的意思是扩展门面本身并不是答案。在大多数情况下,将Facade想象成一个助手函数,例如
auth()->user()
auth::user()
将调用相同的方法,因为它们实际上是将调用委托给底层的
Guard
类。实际上,您需要扩展底层的
Guard
类,并让Laravel使用它。不管怎样,您使用的是什么版本的Laravel?好的……谢谢您的建议。。。我刚刚尝试扩展底层的Guard类: