• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opinions on do/action functions in controllers to handle submitted form data

Im not certain what is the best way to call the type of functions im about to talk about but I just wanted to see if anyone else does a similar method with their application development.

Example - Add a new product to website

Ok, so lets say we want to have a product manager on a site under which in the administration a user can see all of the products and add/edit products. Generally speaking we would have four functions in our controller to achieve this as below.

Please keep in mind this is a very very brief quick example.
function index() {
    $data['products'] = $this->Product->get_all();

function add() {
    $data['mode'] = 'add';
    $data['product']['name'] = '';
    $data['product']['description'] = '';
    $this->load->view('product_addEdit'); // Has a form that submits to do_addEdit function

function edit() {
    $data['mode'] = 'edit';
    $data['product'] = $$this->Product->get($this->uri->segment(3));
    $this->load->view('product_addEdit'); // Has a form that submits to do_addEdit function

function do_addEdit() {
    if ($this->input->post('mode')=='add') {
        // Add product functionality
    } else if ($this->input->post('mode')=='edit') {
        // Edit product functionality

The key things from above are:
- Same view used for adding and editing a product
- When you submit the form to add/edit it's action url is set to a 'do_addEdit' function instead of itself

I am really preferring the building of controller functions like this as I feel it allows for better seperation of the viewing and creating/adding functionality (making it easier to read) and makes for smaller controller functions and helps follow the DRY principle a bit easier.

Does anyone else do their function building similar to our above approach or have any opinions on doing it this way?

[eluser]Sam Dark[/eluser]
Doing almost the same. One view for add/edit but submit method is the same as caller.

Like you i used to reuse the form but now that i discovered the join of convenience i started using separate forms again and i have to say it saves me a lot of headaches i had when there is a difference in displaying the forms

I hardly used a separate function to intercept the form input but now i do because again with the convenience i put the validating and uploading in a 'validation' file, a model where i load the validation library in the constructor. See the second page of the above thread url to see the code.

My controllers now are only used for redirecting if needed.

[eluser]Dave Stewart[/eluser]
Can you explain a little more how you're using models for your validation?

The model does nothing it's just a class i put my validation code in. Because validation has to do with data i think it's more logical in a model than in a library. And by default you can use $this like in controllers which isn't the with libraries.

The chronology of the loading in the front controller is more important. First the model is loaded based on the controller name, then the validation file is loaded so you have access to the model methods for data manipulating and then the method with the page data gets loaded.

Quote:Because validation has to do with data i think it’s more logical in a model than in a library

Your not on your own doing it that way Wink

Our main aims are the controller functions code are kept to be minimal and utilise the models more (eg. we have an admin_model for each module) as it helps save you from repeating code.

[eluser]Dave Stewart[/eluser]
Yeah, I've seen a few people on the forums using models in a slightly less strict manner than MVC might imply you should use them. Then again, as you said, it is data, just not in the "database" form, but that's alright.

[eluser]Dave Stewart[/eluser]
[quote author="Lone" date="1214949959"]Our main aims are the controller functions code are kept to be minimal[/quote]

I'm with you there! It is sometimes tough working out where the hell to put it though.

[eluser]Sam Dark[/eluser]
Personally I like how validation made in CakePHP. You don't have to write lots of code… just need to set up rules in your models:

class Article extends AppModel {
    var $name = 'Article';
    var $validate = array(
        'title' => array(
            'required' => VALID_NOT_EMPTY,
            'length' => array( 'rule' => array('maxLength', 100) )
        'body' => VALID_NOT_EMPTY

[quote author="Dave Stewart" date="1214950129"]It is sometimes tough working out where the hell to put it though.[/quote]

Very true - causes much debate here in the office on where it should go.

eg. In your app you can have products in a category and you want to get all of the products for a give category ID (and you have two models, product_model and category_model)

We would actually get the required information from the category_model because the call is more specific to it. What we have set as a rule is that the model to use is always the model that is more of a parent as such. This can be easily evaluated also by looking at your DB query and seeing what the 'WHERE' specifics are - in this case 'where category is equal to ID 3'.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.