Welcome Guest, Not a member yet? Register   Sign In
Basic CI + MVC + CRUD
#1

[eluser]Madmartigan1[/eluser]
Hello Everyone!

Not 100% sure if this is a CI-specific or MVC question:

Of course there are a million ways to do the same thing in php, but I'm wondering where these things truly belong in a CRUD app. This is not my first day on the job, so please just humor me :-P

Model or Controller?
Form validation/processing
Redirection
Setting status messages/flashdata

I've been doing all of this lately with one magic form processing controller that invokes a model->method by parameters passed to it. Something like this:

Code:
function do_action($model,$action,$id=false)
    {
        //crud methods from models would return true/false
        $this->$model->$action($id)===true ? $status='success' : $status='error';

        //this would load a language line like 'blogs_edit_success'
        $this->session->set_flashdata($status,$this->lang->line($model.'_'.$action.'_'.$status));
        redirect('some_page');
    }

Forms would post to something like 'blog_model/edit_blog/13'
This keeps my app super-tidy, but I feel like it's a bad practice.
However, it seems like a huge waste of time to create a controller function for every form, and bad separation of logic to put validation methods in the controller.
The CI docs even suggest that the form post to itself, which does reduce the need for extra functions, but it seems like validation makes more sense to be in the model.

Sorry, I'm babbling. Once more:

Model or Controller?
Form validation/processing
Redirection
Setting status messages/flashdata

ALSO: Is it better practice to post to a url with the "id" in it, or use a hidden input?

Any comments or suggestions?
#2

[eluser]jedd[/eluser]
{blink}

[quote author="Madmartigan1" date="1267406761"]
Model or Controller?
Form validation/processing
Redirection
Setting status messages/flashdata
[/quote]

Controller, for my money. But get a second opinion. Smile


Quote:I've been doing all of this lately with one magic form processing controller that invokes a model->method by parameters passed to it. Something like this:

Have you looked at extending the core model (MY_Model) and writing some primitives in there for your CRUD work? Do a forum search on my_model for more some hints. [url="/wiki/MY_Controller"]MY_Controller[/url] is well documented, but people often forget that you can do the same with other core bits.

Actually, it sounds like you might be in need of a MY_Controller too ...

Quote:The CI docs even suggest that the form post to itself, which does reduce the need for extra functions, but it seems like validation makes more sense to be in the model.

Which sort of validation? Sanity, xss, constraint, type, or validity (as far as the DB is concerned) ... ?
#3

[eluser]Madmartigan1[/eluser]
@jedd: I've always been told that "fat" models and "slim" controllers is the way to go - and I totally agree. Models are supposed to handle data, which is what the validation lib does. Whether or not we are interacting with a database should not matter.

I agree that redirection and status messages should probably stay in the controller, but what I really want to know is IF there is a method that is generally accepted as "best practice".

I don't want to extend core classes for my situation as I am trying to stay modular. I really didn't mean to focus on that aspect, sorry.

Thanks for the reply!

Any other opinions?




Theme © iAndrew 2016 - Forum software by © MyBB