• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
404 Error On News Tutorial

#1
Hi Everyone, I am pretty much new to Codeigniter and am working through the news tutorial on the Codeigniter website. I keep getting a 404 error when accessing index.php/news. Specifically, I get the following error:

Quote:load->model('news_model'); $this->load->helper('url_helper'); } public function index() { $data['news'] = $this->news_model->get_news(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('templates/footer'); $this->load->view('news/index', $data); } public function view($slug = NULL) { $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'); } }

404 Page Not Found
The page you requested was not found.

Below is all of my code. Does anyone have any thoughts? I appreciate any help.

Thanks!

News_model.php (Model)
PHP Code:
<?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();
    }


News.php (controller)
PHP Code:
<?
class News extends CI_Controller {
    
    public function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('news_model');
        
$this->load->helper('url_helper');
    }
    
    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 NULL)
    {
        
$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');
    }


index.php (View)
PHP Code:
<h2><?php echo $title?></h2>

<?php foreach ($news as $news_item): ?>
    
    <h3><?php echo $news_item['title']; ?></h3>
    <div class="main">
        <?php echo $news_item['text']; ?>
    </div>
    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View
    
<?php endforeach; ?>

view.php (View)
PHP Code:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo 
$news_item['text']; 

routes.php
PHP Code:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view'
Reply

#2
You have <? at the start of your News controller. Do you have short tags enabled?

Try changing it to <?php as normal.
Reply

#3
That worked perfectly. Thank you!

However, now I am getting another error:

An Error Was Encountered

Unable to load the requested file: news/index.php

I double-checked the controller and the routes file and it looks right to me, compared to the tutorial. Any thoughts?

Thanks!
Reply

#4
Bump.

Does anyone have any thoughts on this? Or a good recommendation to a website that can help me research these types of problems?

Thanks!
Reply

#5
(11-21-2015, 11:22 AM)mpar612 Wrote: Unable to load the requested file: news/index.php

Sounds like the view file can't be found. Did you place it in
./application/views/news/index.php
Reply

#6
Awww shoot. It's always the simple stuff. I put it in views, but not views/news.

Thank you so much!
Reply

#7
(11-23-2015, 11:26 AM)mpar612 Wrote: Awww shoot. It's always the simple stuff. I put it in views, but not views/news.

Thank you so much!

You're welcome!
Reply

#8
Awesome! Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.