Php 如何使用内腔中的门立面(Laravel 6.2)
我正在尝试创建一个带有内腔的ACL。 我想使用内置的门/策略来实现这一点。 我一直在关注流明官方文件: 和Laravel Docs: 这样做。他们说我需要登记正面和大门正面才能使用大门 我的AuthServiceProvider.php中有以下代码:Php 如何使用内腔中的门立面(Laravel 6.2),php,laravel,lumen,Php,Laravel,Lumen,我正在尝试创建一个带有内腔的ACL。 我想使用内置的门/策略来实现这一点。 我一直在关注流明官方文件: 和Laravel Docs: 这样做。他们说我需要登记正面和大门正面才能使用大门 我的AuthServiceProvider.php中有以下代码: <?php namespace App\Providers; use App\User; use Firebase\JWT\JWT; use Illuminate\Support\ServiceProvider; use Illum
<?php
namespace App\Providers;
use App\User;
use Firebase\JWT\JWT;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Gate;
//use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function ($request) {
$key = 'pawifjopawiejfpoaiwejfpoji';
$jwt = preg_replace('/^Bearer (.*)/', '$1', $request->header('Authorization'));
$decoded = JWT::decode($jwt, $key, ['HS256']);
return User::where('email', $decoded->email)->first();
});
$this->registerPolicies();
Gate::define('edit-settings', function($user){
return $user->isAdmin;
});
}
}
现在,当我对我的Lumen API使用RESTClient(www.RESTClient.net)运行任何http请求时,我得到以下错误:
Call to undefined method App\Providers\AuthServiceProvider::registerPolicies()
我在谷歌上搜索了很多关于这个问题的信息,找到了一些解决方案,但没有一个对我有效。
请帮助您的提供商没有该方法。这是整个
AuthServiceProvider
,Laravel将其用作AuthServiceProvider
的基础:
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [];
/**
* Register the application's policies.
*
* @return void
*/
public function registerPolicies()
{
foreach ($this->policies() as $key => $value) {
Gate::policy($key, $value);
}
}
/**
* Get the policies defined on the provider.
*
* @return array
*/
public function policies()
{
return $this->policies;
}
}
RegisterPolicys
没有做任何特别的事情。它只是通过$policies
旋转并注册它们;你可以自己做
“与Laravel不同,Lumen在其AuthServiceProvider
上没有$policies
数组。但是,您仍然可以从提供者的引导方法中调用门
门面上的policies
方法”-
因为您的AuthServiceProvider没有该方法。。。它并没有扩展Laravel使用的提供者,它只是扩展了基本服务Provider@lagbox谢谢,但是在lumen代码库中如何/在何处处理此问题?如果您没有任何策略,则没有理由运行RegisterPolicys
,即使它确实存在
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [];
/**
* Register the application's policies.
*
* @return void
*/
public function registerPolicies()
{
foreach ($this->policies() as $key => $value) {
Gate::policy($key, $value);
}
}
/**
* Get the policies defined on the provider.
*
* @return array
*/
public function policies()
{
return $this->policies;
}
}