简体   繁体   English

Laravel API最佳实践

[英]Laravel API Best Practice

I'm building API with laravel 5.1. 我正在使用laravel 5.1构建API。

I have 3 section access: User, Admin, Super Admin. 我有3个部分访问权限:用户,管理员,超级管理员。

I'm curious about the practice for handling the controller. 我很好奇处理控制器的做法。 Right now I make 3 different controller for each section. 现在我为每个部分制作3个不同的控制器。 But sometimes I need to call same function inside the controller. 但有时我需要在控制器内调用相同的功能。 Such as Product:all() . Product:all()

Should I really make 3 different controller or i can use only 2 controller for the best practice?. 我应该真正制作3种不同的控制器吗?或者我只能使用2个控制器进行最佳实践?

I would just use one controller/one route /api/v1/products when the returned dataset is the same for all users (ie for basic users, admins and super admins). 当返回的数据集对所有用户(即基本用户,管理员和超级管理员)都相同时,我只会使用一个控制器/一个路由/api/v1/products

I guess that each admin/super admin is also a user. 我想每个管理员/超级管理员也是一个用户。 So you should protect your /api/v1/products route with a middleware that just checks if the user is an authenticated user, because then he is allowed to access the data. 因此,您应该使用仅检查用户是否是经过身份验证的用户的中间件来保护您的/api/v1/products路由,因为这样他就可以访问数据。

If there is some data other than products that can only be accessed by an admin/super admin, you should create another middleware for that. 如果除了只能由管理员/超级管理员访问的产品之外的某些数据,您应该为此创建另一个中间件。 It is never necessary to create two or more controllers/routes for the exact same API. 永远不必为完全相同的API创建两个或更多控制器/路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM