简体   繁体   English

是否可以在Laravel中结合RESTful控制器和资源控制器?

[英]Is it possible to combine RESTful Controllers and Resource Controllers in Laravel?

I have almost finished developing a website using Laravel 2.4.3 framework. 我几乎已经完成了使用Laravel 2.4.3框架开发网站的工作。

Now we will start developing a mobile application for that website. 现在,我们将开始为该网站开发移动应用程序。 I made a small research on Internet and I found that Laravel supports RESTFUL controllers which I can use to create web services. 我在Internet上进行了一项小型研究,发现Laravel支持RESTFUL控制器,可用于创建Web服务。 Thus, communicate between my server and the mobile app. 因此,在我的服务器和移动应用程序之间进行通信。

My question is: 我的问题是:

is it possible and practical to make the same controller as RESTFUL and resource controller in the same time? 是否有可能和现实 ,使相同的控制器所支持REST和资源控制在同一时间?

or it is better to separate them? 还是最好将它们分开?

Thanks 谢谢

Update 1 更新1

In my opinion, each of them should be a separate controller, I just want to be sure from you as experts guys. 我认为,他们每个人都应该是一个单独的控制者,我只是想确保您像专家一样。

As it is directly stated in the Laravel's own documentation, resource controllers are RESTful controllers around resources. 正如Laravel自己的文档中直接说明的那样,资源控制器是围绕资源的RESTful控制器。 Thus by having a Resource Controller you already have RESTful capabilities, with an addition of resource-handling actions. 因此,有了资源控制器,您已经具有RESTful功能,并增加了资源处理操作。 Laravel Documentation says: Laravel文档说:

Resource controllers make it easier to build RESTful controllers around resources. 资源控制器使围绕资源构建RESTful控制器变得更加容易。 For example, you may wish to create a controller that manages "photos" stored by your application. 例如,您可能希望创建一个控制器来管理应用程序存储的“照片”。 Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller. 通过Artisan CLI和Route :: resource方法使用controller:make命令,我们可以快速创建这样的控制器。

And the actions, too, are listed with the specification of the related HTTP-Verb: 并且相关HTTP-Verb的规范也列出了这些操作:

Actions Handled By Resource Controller

    Verb    Path                        Action                         Route Name
    GET     /resource     index         resource.index
    GET     /resource/create            create                          resource.create
    POST    /resource                   store                           resource.store
    GET     /resource/{resource}        show                            resource.show
    GET     /resource/{resource}/edit   edit                            resource.edit
    PUT/PATCH   /resource/{resource}    update                          resource.update
    DELETE  /resource/{resource}    destroy                            resource.destroy

Though, the above routes are for the example controller given in doc: 不过,以上路线适用于doc中给出的示例控制器:

Route::resource('photo', 'PhotoController');

Therefore, by visiting the domain/resource , if the request is served with POST verb, then the post action would take care of it, otherwise if GET, then the GET action would step in. 因此,通过访问domain/resource ,如果请求带有POST动词,则post操作将处理该请求,否则,如果执行GET,则将执行GET操作。

You can read about it here: 你可以在这里读到它:

http://laravel.com/docs/controllers#resource-controllers http://laravel.com/docs/controllers#resource-controllers

EDITED EDITED

On your question about separation, I should say, as already stated, Yes. 关于您的分离问题,我已经说过,是的。

You have controllers which are responsible to handle resource management and manipulation, while there are times when you need RESTful controllers to take care of your requests in different way. 您有负责处理资源管理和操纵的控制器,而有时您需要RESTful控制器以不同的方式处理您的请求。

For instance you have a controller which deals with data-processing and calculation, then, it does not have anything to do with a Resource management. 例如,您有一个处理数据处理和计算的控制器,那么它与资源管理没有任何关系。

or it is better to separate them? 还是最好将它们分开?

Yes. 是。 Always. 总是。

Automagic in routing is the devil, and I have grown to hate it over time. 路由的自动魔术是魔鬼,随着时间的流逝,我已经变得讨厌它。

http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil

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

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