3

Following along with the CI News Tutorial

I'm only doing the news section so I changed the default controller to 'news'

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['default_controller'] = 'news';

Now a 404 error is generated from the 'View article' anchors. Changing the default to:

 $route['default_controller'] = 'welcome';

creates the correct path. How should I change the router to use news?

No custom config, or .htaccess used.

From config.php:

$config['base_url'] = 'http://frameworks:8888/ci_site_tut/';

$config['index_page'] = 'index.php';

News Controller:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 
*/
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');
    }

    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create A News Item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE) 
        {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');
        } else {


            $this->news_model->set_news();
            $this->load->view('news/success');

        }



    }

}

SOLUTION:

Autoload URL Helper:

$autoload['helper'] = array('url');

Updated routes:

$route['default_controller'] = 'news';
$route['404_override'] = 'errors/page_missing';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';

Updated URL in views/news/index.php:

<p><a href="<?php echo site_url('news/' . $news_item['slug']); ?>">View Article</a></p>  
3
  • What is the url that you're trying to use in your browser? Commented Jan 3, 2012 at 17:04
  • <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>. index.php is not included in the URL. Commented Jan 4, 2012 at 17:39
  • I had the same issue.... I think it's a typo. I changed <a href="news/ to <a href="view/ and everything worked fine. Commented Oct 15, 2013 at 23:50

3 Answers 3

1

It doesn't look like you are setting $new_item['slug'] anywhere in your view method. do a 'print_r($data['news_item']) in your view method just before you load your views to see if you actually are setting the slug.

if you're sure you are, make sure the url helper is autoloaded and try using this for your url in your view.

<a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Your suggestion worked, and was also recommended by the CI site. also updated my routes.
0

You don't need any of those custom routes, just a view controller. Set index page to blank and remove the base URL. You sshould just need a create and view method in your news controller.

2 Comments

I've a view controller per the tutorial, The custom routes are not needed due to having only one controller(News)? Thanks for the reply!
The suggestion still returns a 404.
0

I also ran into the 404 page not found routing error. So I think my answer will help some users who encounter the same problem. Here is my two cents.

If one has exactly followed the CI news tutorial, it is highly likely to get into the above trouble. To avoid the error what you should do is have the route configuration in the order exactly given below:

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = "pages/view";
$route['404_override'] = '';

Because the people following this tutorial most likely come from the preceding tutorial page and append the new route configurations to the already existing ones which run them into the trouble.

And other thing is, in the view method of the News controller, change the line

$data['news'] = $this->news_model->get_news($slug);

to

$data['news_item'] = $this->news_model->get_news($slug);

They should have emphasized the order of the route configurations and removed the typo in the code. Hope this helps.

Comments

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.