正在播放:API 接口开发 - topic 话题更新接口的实现
更新时间:1年前
Laravel API 接口开发
标 题 | 时 间 |
---|---|
需要使用到OAuth 密码授权模式的 API 接口开发 | 02:24 |
学习API开发前的准备工作 | 03:55 |
API 开发 - 用户注册接口的实现 | 10:23 |
Passport API 开发辅助授权包的安装和基本配置 | 07:07 |
API 开发之用户登录接口的实现 | 05:42 |
API 接口开发:完善用户注册流程 | 04:59 |
API 接口开发-使用用户名或手机号码作为登录凭证 | 03:05 |
API 接口开发 - Topic 话题创建接口的实现 | 08:13 |
API 接口开发 - 使用 transformer 对 topic 话题数据进行转化 | 04:34 |
API 接口开发 - topic 话题查看接口的实现 | 08:04 |
API 接口开发 - topic 话题更新接口的实现 | 11:05 |
API 接口开发 - topic 话题删除接口的实现 | 03:41 |
API 接口开发 - discussion 讨论观点发布接口的实现 | 13:56 |
API 接口开发 - 使用 transformer 让 topic 携带关联的 discussions 数据 | 02:26 |
API 接口开发 - 使用多样化的一对多创建扩展性更强的点赞系统 | 08:03 |
API 接口开发 - liking 点赞接口的实现(1) | 14:10 |
API 接口开发 - liking 点赞接口的实现(完整逻辑的实现) | 12:17 |
API 接口开发 - 对返回的错误信息数据进行统一的规范 | 09:49 |

llkllc 2019-06-11 00:12:44
$this->middleware('auth:api')->except(['index', 'show']); $this->authorizeResource(Topic::class); 水平有限也要来发表一下。auth中间件,有解析access_token并登陆用户的功能(测试方法:打开/关闭auth中间件的except,postman带上token,在index或者show方法中打印user信息),在auth中间件上except了index和show,所以并没有登陆用户,因此在authorizeResource中,在某一步获取用户信息时失败了,没有真正走到TopicPolicy。跟踪到Illuminate\Auth\Access\Gate.php@raw方法下有$user信息,就头大了。


小人物1号 2020-03-31 13:25:36
关于 TopicPolicy.php View 方法不好使的问题做了一些调查, 用的 laravel 7 第1种情况是, 用户登录后, view 使用正常 第2种情况是, 让 view 和 index 不做权限验证, 跳过验证部分 下面介绍的是第2种 $this->authorizeResource(Topic::class); 调查发现,这段代码的作用就是, 帮助我们调用几个方法,如下: $this->middleware('can:viewAny,App\Model\Topic')->only(['index']) $this->middleware('can:view,topic')->only(['show']) $this->middleware('can:create,App\Model\Topic')->only(['create','store']) $this->middleware('can:update,topic')->only(['edit','update']) $this->middleware('can:delete,topic')->only(['destroy']) 可以推断出是中间件的使用问题, 又看到 authorizeResource 方法的第三个函数是一个和 $this->middleware('auth:api')->except(['index','show']); 中的 except方法很像的参数, 最终效果如下, 测试正常 $this->authorizeResource(Topic::class, null, ['except'=>['index','show']]); 最终生成 $this->middleware('can:viewAny,App\Model\Topic')->only(['index'])->except(['index','show']) $this->middleware('can:view,topic')->only(['show'])->except(['index','show']) $this->middleware('can:create,App\Model\Topic')->only(['create','store'])->except(['index','show']) $this->middleware('can:update,topic')->only(['edit','update'])->except(['index','show']) $this->middleware('can:delete,topic')->only(['destroy'])->except(['index','show']) 因为刚开始学习php , 只测试了 laravel 7 的版本, 其他的版本就不清楚了

vince 2020-08-12 11:09:03
Laravel7中可以通过声明一个「可选」类型提示或为用户参数定义提供一个 null 默认值 namespace App\Policies; use App\Post; use App\User; class PostPolicy { /** * Determine if the given post can be updated by the user. * * @param \App\User $user * @param \App\Post $post * @return bool */ public function update(?User $user, Post $post) { return optional($user)->id === $post->user_id; } }

暂无相关资源