Welcome Guest, Not a member yet? Register   Sign In
CONTROLLERS, single method or different methods for different actions?
#1

(This post was last modified: 05-10-2016, 06:27 AM by CINewb.)

Let's say I have a Controller and a View where you can edit a customer (where 123 is the customer's unique ID):

/customer/edit/123

The view might be made up of some regular form fields, for example Name, Email Address, Telephone Number, etc.  The database table in the related model might be called "customers".

But say the View also features a "sub table" - for example a list of the customer's favourite colours.  I want to be able to add colours to the list, delete colours from the list and change the order of preference of the colours.  I'd also want to be able to edit the list of colours via Ajax.  There would be a separate database table in the model perhaps called "customer_colours"

This leaves me with one of two options:

1) In the view I could have a single form which posts to /customer/edit/123 and then do some jiggery poker to figure out whether the user is attempting to edit the core customer fields, or whether they're adding/editing/deleting a colour, and whether the request is a regular one or an Ajax call.

I tried this and I ended up with a humongous Controller method.  I tried breaking it down into a master controller method which then calls private sub methods depending on the required action, which was better but still somewhat of a mess.

2) The other option is to create multiple form tags in the View, such as /customer/edit, /customer/colour_delete, /customer/colour_add, etc which then correspond to the appropriate methods in the Controller, but all of which output the same View.  This feels a lot more organised, but there are then limitations as to how the forms in the View can be laid out.

Is there a method which is more preferable over another?
Reply


Messages In This Thread
CONTROLLERS, single method or different methods for different actions? - by CINewb - 05-10-2016, 06:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB