- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Laravel 5.2。所以我正在学习如何处理角色和权限 Authorization .一切运行良好。我什至制定了自己的政策 PostPolicy。
现在开始讨论问题。我将 $post 数据加载到 PostsController 的 View 中,然后将其加载到 Blade 中。
帖子 Controller :
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
posts/show.blade.php:
@section('content')
<!-- begin -->
@can('hasRole', Auth::user())
<h1>Displaying Admin content</h1>
@endcan
@can('hasRole', Auth::user())
<h1>Displaying moderator content</h1>
@endcan
@can('hasRole', Auth::user())
<h1>Displaying guest content</h1>
@endcan
政策:
public function hasRole($user)
{
// just for test
return true;
}
现在返回所有内容。
当我更改 @can('hasRole', Auth::user())
从 Auth::user() 到一个字符串,即
@can('hasRole', 'guest')
<h1>Displaying guest content</h1>
@endcan
在这种情况下,它不会返回任何内容。由于我是 Laravel 的新手,我真的不知道它行不通。
最佳答案
您可能没有仔细阅读文档。您应该将模型作为第二个参数传递,而不是字符串或用户对象。在你的情况下,你可能应该使用这样的东西:
@section('content')
<!-- begin -->
@can('hasRole', $post)
<h1>Displaying Admin content</h1>
@endcan
@can('hasRole', $post)
<h1>Displaying moderator content</h1>
@endcan
@can('hasRole', $post)
<h1>Displaying guest content</h1>
@endcan
但问题是您真正想要实现的目标。如果您只想使用用户角色来验证权限,则不需要使用此指令。
您可以添加到您的 User
模型函数来验证当前角色,例如
public function hasRole($roleName)
{
return $this->role == $roleName; // sample implementation only
}
现在您可以在 Blade 中使用:
@section('content')
<!-- begin -->
@if (auth()->check())
@if (auth()->user()->hasRole('admin'))
<h1>Displaying Admin content</h1>
@elseif (auth()->user()->hasRole('moderator'))
<h1>Displaying moderator content</h1>
@endif
@else
<h1>Displaying guest content</h1>
@endif
关于php - Laravel blade @can 策略 - 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457983/
我知道 Laravel 的 Blade 模板位于/resource/views 目录中。它的扩展名是 .blade.php 。 我的问题是我已经为前端设计人员设置了一个单独的 repo 来处理 bla
我知道 Laravel 的 Blade 模板位于/resource/views 目录中。它的扩展名是 .blade.php 。 我的问题是我已经为前端设计人员设置了一个单独的 repo 来处理 bla
在我的数据库中,我保存包含 Blade 标记的文本,例如: Hello {!! $name !!} how are you today. 我将此文本传递到变量 $text 中的电子邮件模板。在我使用的
我想从3个表构建 Blade View : “inputs_details”-字段:article_type(值:“p”代表产品,'s'代表服务),article_id,...。 “产品”-字段:ID
我正在使用 Laravel 5.1。我正在尝试将 Blade 指令 (@extend) 与我的自定义 Blade 指令一起使用。 Blade::directive('base', function()
我的 Blade 中有一个表格,其中包含 2 个日期供用户选择,例如: {{csrf_field()}} From
我正在尝试使用表单生成器创建一个按钮 {!! Form::button(' 'yourstyle']); 关于php - 在 Blade 代码中插入 Blade 代码,我们在Stack Overflo
我正在渲染一个页面,该页面主要是 Laravel 中带有 view::make 的表单,它正在崩溃,导致 ERR_CONNECTION_RESET。经过长时间的调查和许多红鲱鱼,我开始从 Blade
我有以下路径的部分 View 。 /views/acp/review/check.details.blade.php 我如何使用 Blade 包含这样的文件,因为如果我这样做 @include('vi
我继承了一个 Laravel 项目,该项目有许多 Blade 文件, Blade 文件中的标签内带有 javascript。问题是有很多重复的 JS 逻辑,所以我想提取 JS 并创建 JS 文件以包含
我正在为 laravel 项目中基于 Blade 的组件创建文档,并希望将语法突出显示的 Blade 代码片段显示为文档的一部分,就像这样: 我已经安装了 graham-campbell/markdo
在我激活 Facade 的 Lumen 应用程序中($app->withFacades() in app's bootstrap)我无法在 Blase 模板中使用 URL 类。 {{ URL::pre
我正在为 laravel 项目中基于 Blade 的组件创建文档,并希望将语法突出显示的 Blade 代码片段显示为文档的一部分,就像这样: 我已经安装了 graham-campbell/markdo
在我激活 Facade 的 Lumen 应用程序中($app->withFacades() in app's bootstrap)我无法在 Blase 模板中使用 URL 类。 {{ URL::pre
有人知道是否可以延长 child Blade 吗? 我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列
我试图在 blade 中设置一个 php 变量。它可以在同一个 Blade 上工作,而不是在其他 Blade 上设置 var。 例子: header.blade.php @if(isset($comp
情况: 我正在制作一个侧面菜单,该菜单通过从表 (MySQL) 中获取数据来填充。 每代表菜单的一项: item 1 item 2 item 3 等等... 问题: 当我看到它的 Blade 时一切正
我在主视图中有 3 个元素 标题(导航栏) 内容 页脚 在 app.blade.php 中 @include('header') @yield('content')
我想将输入值从一个 Blade 文件传递到另一个 Blade 文件。 我是 PHP Laravel 的新手,在尝试使用它时遇到错误。 我认为我的语法在这里是错误的。有人可以帮忙吗? channel .
所以我的困境是这样的。 Email: info@example.com 正在作为 Blade 代码处理,不会在我的 Laravel 4 框架中的响应式 Bootstrap 网页中调整大小。 关于如何让
我是一名优秀的程序员,十分优秀!