简体   繁体   中英

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.

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. 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?

or it is better to separate them?

Thanks

Update 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. Thus by having a Resource Controller you already have RESTful capabilities, with an addition of resource-handling actions. Laravel Documentation says:

Resource controllers make it easier to build RESTful controllers around resources. 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.

And the actions, too, are listed with the specification of the related 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:

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.

You can read about it here:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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