lundi 19 septembre 2016

Multiply routes or branch in controllers? (using laravel 5)

I'm building a cms type app, with routes/views provided firstly for users who are also editors which let them add and edit content.

Also, another set of routes exist which simply serve the content once its been finalised, with no option to edit (this being the only set of views delivered to users who are not editors).

On the one hand, since I can name all these routes after the content hierarchy itself, initially I thought of creating an 'edit' subdomain or route prefix which would not be accessible to non-editors.

But I'd appreciate viewpoints on pros/cons/feasibility of using a single set of routes/endpoints (i.e. using an identical route and identical controller for edit vs. "see it only") and simply having the controller switch which view it responds with based on the type of user, editor or not editor.

On the one hand, it is nice to differentiate the very different views by separating them entirely as different routes.

On the other, on the principle of parsimony, if one has the opportunity to halve the number of routes your application uses, (and perhaps reduce duplicated code), that seems a very nice goal too.

A related question seems to be the same re. APIs: if I understand correctly its often advisable to differentiate one clearly behind an /api/v1 type route postfix. But this said, are there any common use cases where you have a single route & controller branch on some condition to serve either html or json? Just curious.

In writing this, I'm realising I can put branching logic within a view itself, based on user type (though this seems a bit odd to me in general and perhaps better in more exceptional use cases), but then again it appears the question I have to forecast is do I want my edit view to be dramatically different to the non editors view, or very similar.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire