Manish Kumar
Manish Kumar

Reputation: 1469

function view in controller codeigniter

I am working on the news section example as demonstrated in the link below: http://www.codeigniter.com/userguide2/tutorial/news_section.html

It contains two functions in the controller index and view. When I remove the view function from my controller even then I get the same output. Can any one of you help me understanding the need of function view in the controller?

<?php
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }

   public function index()
   {
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }
    public function view($slug)
    {
        $data['news_item'] = $this->news_model->get_news($slug);

        if (empty($data['news_item']))
        {
            show_404();
        }

        $data['title'] = $data['news_item']['title'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }


}

Upvotes: 0

Views: 1485

Answers (1)

gen_Eric
gen_Eric

Reputation: 227200

How CodeIgniter URLS work is the following:

example.com/controller/method/param1[/param2...]

From: http://www.codeigniter.com/userguide2/general/urls.html

When you go to yoursite.com/news, it automatically runs the index() function. But, what if you went to yoursite.com/news/view/1234?

Then it would run your view() function and pass '1234' as a parameter ($slug).

Upvotes: 2

Related Questions