1

I saw plenty of questions and tried all those fixes similar to this issue but could not get this resolved. Please help.

I am following codeigniter news item tutorial. I displayed news from database. When I click on a link to view particular news it throws 404.

Here is the code:

Controller:

    <?php

class News extends CI_Controller {

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

    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);
     //   var_dump($data);exit;
        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');
    }

}

Model:

<?php

class News_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_news($slug = FALSE) {
        if ($slug === FALSE) {
            $query = $this->db->get('news');
            return $query->result_array();
        }

        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    }

}

View:

news.php

<?php

class News extends CI_Controller {

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

    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);
     //   var_dump($data);exit;
        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');
    }

}

view.php

<?php

echo '<h2>' . $news_item['title'] . '</h2>';
echo $news_item['text'];

routes:

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

Base url is blank in config.

CI version is 2.1.4 (latest)

This URL shows all the news items correctly - http://localhost/educon/index.php/news/

This URL throws 404 - http://localhost/educon/index.php/news/view/n1

aBhijit
  • 5,261
  • 10
  • 36
  • 56

6 Answers6

1

The problem is in your routes. Your routes file have the line as

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

But your url is

http://localhost/educon/index.php/news/view/n1

This will redirect to

http://localhost/educon/index.php/news/view/view/n1

In your view($slug) method you have a parameter. You dump this $slug in first line of view() and check it.

Casperon
  • 107
  • 12
  • "You dump this $slug in first line of view() and check it." - Please elaborate this part. I don't know what you mean by this. – XT_Nova Jun 12 '14 at 10:13
  • @getJETsetTER it means you print the $slug parameter in the first line of the view() function – Casperon Jul 02 '14 at 07:31
1

I had a similar issue. For me, the routes.php file was fine (by "fine" I mean it matched what the tutorial said to do (also version 2.1.4)), but my views/news/index.php file generated the news article link like so:

<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>

After noting the issue with the generated path, I dropped news from the path and this worked:

<p><a href="<?php echo $news_item['slug'] ?>">View article</a></p>

So links went from: /news/news/news-item-2 to /news/news-item-2

And given the tutorial's routing:

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

That then made sense.

Charlie Hills
  • 636
  • 6
  • 13
1

I encountered this just now, and I also just copied everything in the tutorial, the thing is, it is in the arrangement of the routing, at first, I just added the new codes in routes.php like this....

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

then change it to

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

And now it works like a charm...

1

Using slug without spaces worked for me.

The 'News Section' part of the tutorial tells you to create some seed records. In my case, my slug contained spaces and I got the 404 error. Removing the spaces fixed this. (CodeIgniter v.3.1.4)

seeker82
  • 11
  • 1
  • 1
0

try to check your link that goes to news listing it should be something like this

<li><a href="<?php echo base_url(); ?>news/index">News</a></li>

you need to change it to read as follows, because i tried it out and its the fix for the problem

<li><a href="<?php echo base_url(); ?>news/index/index">News</a></li>

It will be great if someone with more experience can guide on how to correct this in the route.php

below is my route.php

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

note it will work even if you make the second line as:

$route['news'] = 'news';

and .htaccess is as:

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

best

odins
  • 111
  • 1
  • 5
0

I have a similar problem, but now with this version of Routes.php in CodeIgniter4 tutorial :

$routes->get('/', 'Home::index');
$routes->get('news', ['App\Controllers\News', 'index']);
$routes->get('news/(:segment)', ['App\Controllers\News', 'view']);
$routes->get('pages', ['App\Controllers\Pages', 'index']);
$routes->get('(:segment)', ['App\Controllers\Pages', 'view']);

I don't know why it is giving a 404 for an individual news.

CBD
  • 21
  • 4