Laravel 7:如何限制用户的URL?
我是一个正在学习Laravel 7的新手。我已经开发了一个使用Laravel 7的小型Web应用程序。但是今天我注意到了一个问题。所有URL都是全局的,这意味着所有用户都可以访问我网站的所有URL。假设用户一个创建的数据,并在每个行中存在编辑和删除按钮的表中显示。编辑URL就像:localhost/Records/Records/Edit/5
。问题是,其他登录的用户也可以访问此编辑页面。这样,所有登录的用户都可以访问所有URL,这非常糟糕。 我希望你明白我在说什么。我有将近250个以上的网络路线。有什么简单的限制路线的方法吗? 用户只能访问自己的数据。我该怎么做? 谢谢
I'm a newbie who is learning Laravel 7. I have developed a small web application with Laravel 7. But today I noticed one problem. That all the URLs are global, means all users can access all the URLs of my website. Suppose User A created data and it shows in a table where the edit and delete buttons exist with every row. The edit URL is like: localhost/records/edit/5
. The problem is, that other logged-in users can access this edit page also. Like this, all the URLs are accessible by any logged-in users which is very bad.
I hope you understand what I'm saying. I have almost 250+ web routes. Is there any easy way to restrict the routes?
User can access their own data only. How can I do that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须注册策略,并确保未经正确的授权,用户无法访问网站的一部分。
请参阅 docs 关于如何制定政策和实施它们。
示例代码:
策略:
控制器
记录索引
You'll have to register policies and ensure users cannot access parts of the website without the correct authorization.
See the docs on how to write policies and implement them.
Sample code:
Policy:
Controller
Records index
create_by
created_by
field in user_table DB如果我正确理解您,您想将路由限制为特定用户。
IE将角色_ID添加到用户表中。
管理中间件:adminmiddleware.php
in kernel.php
您也可以使用spatie软件包
。 /middleware“ rel =” nofollow noreferrer“> https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware
只需检查角色是否允许使用该路线:
If i understand you correctly you want to restrict routes to specific user.
i.e add role_id to users table.
Admin Middleware: AdminMiddleware.php
In Kernel.php
You Can also Use Spatie package for this.
https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware
Just Check If Role is allowed to use that route: