0

I am learning code Igniter and am having a hard time following the MVC pattern. Below is the scenario.

MODEL: article

public function get_all_ideas(){
    $this->db->select('article_title, article_content');
    $query = $this->db->get('article');
    return $query;
}

Here, I have created a function to extract all the articles from the table.

CONTROLLER:

public function articles(){
    $this->load->model('article');
    $data['articles'] = $this->article->get_articles();

    $this->load->view('all_article', $data);
}

Here, I have created a function in controller which loads the model and passes the query results to the view file, 'all_article'.

VIEW: all_article

<html....
    <?php
    foreach($articles->results() as $article){
        ?>
        <div id="box">
            <?php echo $article->article_content;?>
        </div>
    <?php  
    }
    ?>
....</html>

I think I have followed MVC pattern in a proper way in the above code. View file will now display all the content of the articles. I have set the css of 'box'

<div id="box">

in such a way that they are kind of small preview box. Also, I have found that there is a text helper, word_limiter() in CI which limits the words. Since these boxes only show preview of article, I planned on using word_limiter(). Now here's the problem.

If I were to limit the word. Then I will be loading a helper class in the SAME VIEW FILE like this

VIEW:

<div id="box">
    <?php
        $this->load->helper('text'); 
        echo word_limiter($article->article_content, 40);
    ?>
</div>

You might have noticed that I am loading the helper class in the VIEW file, which is quite not right according to the principle of MVC. Since such logic has to be in CONTROLLER. But since, the method word_limiter() has to be called in each loop, I didn't see a way to fit this into the CONTROLLER. And I know, doing this violates the principle of MVC.

How should it be done? need help

Ajay
  • 2,022
  • 19
  • 33
Nirmalz Thapaz
  • 925
  • 4
  • 13
  • 28
  • You don't need to load the helper in view you can use `config/autoload.php` and edit this `$autoload['helper'] = array('text');` then just use `word_limiter` without loading it also it will be available to your whole application. – Rahil Wazir Feb 09 '14 at 16:19
  • 2
    Load the helper within the Controller, use the function within the view. but **FYI: CodeIgniter has no idea about MVC**. It doesn't implement even **MVP**. – Hashem Qolami Feb 09 '14 at 16:52
  • you can also load helper in your controller function as you do in view. i think the pattern iss right – Ranjeet Singh Dec 17 '14 at 08:10

1 Answers1

1

just use autoload (application/config/autoload.php). you can autoload that text helper, then use it everywhere you want.

Yoga Sukma
  • 34
  • 4
  • 1
    Please note that [only the necessary helpers or libraries should be loaded automatically](http://stackoverflow.com/questions/21162136/codeigniter-autoloaded-model-variables-reset/21162534#21162534). Those you really need to use them everywhere. – Hashem Qolami Feb 09 '14 at 16:55
  • 1
    @HashemQolami Exactly, only the necessary helpers/libraries should be loaded automatically. Since, I planned to use this particular function in only one view file, I chose not to autoload the class. Instead what I did was loading the necessary library within the controller which invoked this particular view file -- as you said. I didnt think about this, I didnt know that the library loaded in the controller will still be available in its corresponding view file. Appreciate that you pointed this out for me !! btw, you did tell me this through comment. – Nirmalz Thapaz Feb 10 '14 at 07:52
  • @NirmalzThapaz When you use `load->view()` method, it simply includes the view page into the script, hence the pre-loaded helpers would be accessible within the view. I didn't post this as an answer because these general questions are *primarily opinion-based* topics :) – Hashem Qolami Feb 10 '14 at 09:15