0

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');
    }


}
1
  • Both codeigniter version of user guides can be found here now. codeigniter.com/docs Commented Oct 23, 2015 at 5:39

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

so you are saying it is just to display the right things if 1234 exists ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.