Wai*_*ein 5 laravel laravel-nova
我正在使用 Laravel 开发 Web 应用程序。我正在使用 Nova 作为管理面板。我现在正在做的是使用文档中提到的策略授权我的资源。但似乎它不起作用。这是我迄今为止所做的。我已经创建了一个这样的 nova 资源。
class Item extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \App\Models\Item::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
Run Code Online (Sandbox Code Playgroud)
然后我为该资源创建了一个名为 Item 的 Laravel 模型类。
然后我创建了策略。
class ItemPolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
return true;
}
public function view(User $user, $item)
{
return true;
}
public function create(User $user)
{
return false;
}
public function update(User $user, $item)
{
return false;
}
public function delete(User $user, $item)
{
return false;
}
public function restore(User $user, $item)
{
return false;
}
public function forceDelete(User $user, $item)
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在 AuthServiceProvider 中注册了该策略。
protected $policies = [
Item::class => ItemPolicy::class,
];
Run Code Online (Sandbox Code Playgroud)
当我在 nova 管理面板中看到项目列表时,我仍然可以创建项目。怎么了?创建项目的选项应该是隐藏的。
小智 -1
可能是因为您在方法参数中缺少模型类型
添加Item $item传递 $item 的所有方法,如下所示:
public function update(User $user, Item $item)
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
您还可以排除所有您希望不可用的方法,默认情况下它们将被禁用
| 归档时间: |
|
| 查看次数: |
4253 次 |
| 最近记录: |