Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Routing-404 error

Tags:

codeigniter

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>  
like image 324
Wasabi Avatar asked Mar 23 '26 02:03

Wasabi


1 Answers

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>
like image 72
Catfish Avatar answered Mar 26 '26 13:03

Catfish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!