问题描述
Laravel 8
中的外观在控制器文件中完美运行,但我想在刀片文件中使用外观功能不起作用
错误:
Error Call to undefined method App\PaymentGateway\PaymentFacade::process() (View: D:\alpha\resources\views\admin\auth\register_coverage.blade.PHP)
{{ \Payment::process() }} //// this is not working
控制器文件
public function registerStore(Request $request) {
dd(Payment::process()); ///// this is working
}
我想知道这个问题的解决方案。
付款文件
<?PHP
namespace App\PaymentGateway;
class Payment {
public static function process(){
return "testing";
}
}
PaymentFacade 文件
<?PHP
namespace App\PaymentGateway\Facades;
class PaymentFacade {
protected static function getFacadeAccessor(){
return 'payment';
}
}
PaymentServiceProvier 文件
<?PHP
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\PaymentGateway\Payment;
class PaymentServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('payment',function(){
return new Payment;
});
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
配置/应用文件
'providers' => [ App\Providers\PaymentServiceProvider::class,],'aliases' => [ 'Payment'=> App\PaymentGateway\Facades\PaymentFacade::class,
解决方法
您应该扩展基础 Facade 类。将 App/PaymentGateway/Facades/PaymentFacade.php 更改为
<?php
namespace App\PaymentGateway\Facades;
use Illuminate\Support\Facades\Facade;
class PaymentFacade extends Facade {
protected static function getFacadeAccessor(){
return 'payment';
}
}