#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Where is the proper place to apply string manipulation functions in mvc?


    I wanna apply some functions like ucwords to title for example. Where is the proper place to apply?
    M
    PHP Code:
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Model_categories extends CI_Model {

        public function 
    list_categories()
            {
                
    $this->db->select('id, title');
                
    $this->db->from('atless_categories');
                
    $this->db->order_by("title"); 
                
    $query $this->db->get();
                return 
    $query->result_array(); 
            }

    V
    Code:
    <select>
        {categories}
            <option value="{id}">{title}</option>
        {/categories}
    </select>
    C
    PHP Code:
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Categories extends CI_Controller {
        public function 
    index()
            {
                
    $this->load->model('model_categories');
                
    $categories $this->model_categories->list_categories();
                
    $data = array('categories'=>$categories);
                
    $this->parser->parse('view_categories'$data);
            }

    Northie in case you see I forgot to respond to you before about if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    Somebody could try and directly access one of code files, rather than going through CIís code. For example, if you have a library called foo.php, somebody might try to access it through: yoursite.com/system/application/libraries/foo.php. As far as I can say this is done to prevent any hassles of code running when it shouldnít. foo.php probably assumes that the rest of the framework has been loaded. If itís run by itself, strange errors could result that would either confuse users or display sensitive information.
    Making sure BASEPATH is defined is just a simple way to know that CI has been loaded.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Depends. Why are you capitalizing it? Because the data is wrong in the model and it actually should be capitalized? Because you want the view to show it like that just to the user? Because there's some sort of logic in the controller that depends on there being capital letters?
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by requinix
    Depends. Why are you capitalizing it? Because the data is wrong in the model and it actually should be capitalized? Because you want the view to show it like that just to the user? Because there's some sort of logic in the controller that depends on there being capital letters?
    Mostly becuse I want the view to show it like that just to the user.

    But you made me curious about the rest of the items above as well. Please explain if you dont mind.

    Thank you.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    First you should consider whether the raw data itself should be capitalized too. If you find yourself ucfirst-ing it everywhere then it should probably be like that to begin with.

    Anyway, three places to do it for three different reasons:
    1. Do it in the model if the data should be capitalized to begin with. Basically what I said above.
    2. Do it in the controller if there's some special logic that depends on the right letter case for the strings. I can't really think of any situation where you'd do it here but I threw this in for completeness. See also #3 because the line between controller and view is very much open to interpretation and different people will tell you different places to draw that line.
    3. If the capitalization is purely cosmetic and you don't want to change the underlying data to fit, putting it in the view is the best place. In my opinion: the view is where the capitalized version is used so that's where the transformation should take place. Other people will say that the view should have, like, zero logic in it at all and that the controller should do all the work preparing the data for presentation. (And other people will say you need a layer for business and/or view models and that's where the capitalizing should go.)

    Another way to look at it is what kind of code the controller will use.
    PHP Code:
    #1. put it in the model if you would write code like
    $category ucwords($model->category);
    // ignores the original value, ucfirst()s it immediately

    #3. put it in the view (or controller) if you would write code like
    $category $model->category;
    // ...
    $this->view->category ucwords($category);
    // you used $category as it was but passed the capitalized version to the view

    #2. put it in the controller for... well, pretty much anything else 
    Also consider what you'd have to do to your existing code:
    1a. (Database) Model might need changes to an "insert" or "update" method so that it puts the data in correctly. Otherwise the "select" methods are the same.
    1b. (Model) Model's "insert" and "update" are the same but the "select" needs to ucwords() the value it gets.
    2. (Controller) You'd have to alter the $categories array you got from the model and that'll require a loop.
    3. (View) Assuming the templating language supports it, make it invoke ucwords().

IMN logo majestic logo threadwatch logo seochat tools logo