• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial error...?? (RESOLVED)

#1
[eluser]Matteo[/eluser]
From a new CI user...

I was following the tutorials and got the first part of the news page to work, but when I click on the "slug" entry to go to the second single-post page, I get CI's 404 error.

My model code is as follows:
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();
}
}
My View code (/views/news/index.php) is as follows:
Code:
<?php foreach ($news as $news_item): ?>

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    <p><a hre="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>

&lt;?php endforeach ?&gt;
and my view code for the single page (/views/news/view.php) is as follows:
Code:
&lt;?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
My controller code is:
Code:
&lt;?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'] = $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');
  }
}
Just for good measure, I have included my routes file as well...
Code:
*/

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
//$route['404_override'] = '';
I would have posted the HTML, but the forum won't let me...

#2
[eluser]Aken[/eluser]
What's the URL of the page giving you the 404 error? You can use example.com instead of your live domain.

A couple things you can try:

1) Load the URL helper to your controller constructor, and use the site_url() function to help generate the news slug URL.
Code:
// In News __construct():
$this->load->helper('url');

// Replace your link URL with:
&lt;?php echo site_url('news/' . $news_item['slug']); ?&gt;

2) In your controller's view() method, replace show_404() with something like
Code:
exit('The error is here.');
If your browser then displays the error message instead of CI's 404 page, then you know your model isn't returning the data it's expected to.

#3
[eluser]Matteo[/eluser]
The URI of the page is

Code:
http://domain.com/index.php/news/slugname

if that helps...

I will try your suggestions and let you know, thnx.

+++++++++++++++++

I just made the changes to my code that you suggest and I still get the same internal CI 404 error when I try to view
Code:
http://domain.com/index.php/news/slugname

Help! I'm tearing my fingernails out!

#4
[eluser]Aken[/eluser]
Whoops, sorry. The URL for viewing a news article the way you have it set up now would be:
Code:
domain.com/index.php/news/view/slug

If you want to use the URL structure with "news/slug", then you need to add a route:
Code:
$route['news/(:any)'] = 'news/view/$1';

#5
[eluser]Matteo[/eluser]
The route was already there.

I gave up and re-did the entire tutorial from the beginning. It is still borked.

Code:
http://www.domain.com/index.php/news
now shows up as a blank page; no 404 error, CI or apache. I'm back to where I started with no clue as to why this is happening.

#6
[eluser]Aken[/eluser]
A blank page means there is an error somewhere, but you have error reporting turned off.

The CI tutorials out there are incredibly simple. Maybe you should learn more about PHP before trying to use CodeIgniter.

#7
[eluser]Aken[/eluser]
In your routes, define your default controller and error controller rules before anything else.

#8
[eluser]Unknown[/eluser]
What fixed the issue? I ran into the same thing.

#9
[eluser]Benair[/eluser]
I was trying to work through the Tutorial and ran into the same issue and ended up resolving it in my example. After a lot of head-banging and trial and error, I discovered this was an issue in my own setup.

I didn't know what the "Slug" represented so I typed in "Sample Slug 1:", etc...
I changed the text that I had put in the sample records in the Slug field and got it working. If you put more than one word in the slug it breaks on the spaces (because it uses the slug for the filename in the URL) also if you put a colon(Smile in the slug it breaks etc. so maybe this will help others to resolve it.

I've worked with Procedural PHP for a long time at my job having to fumble along and learn as I go, so admittedly I dont understand objects and frameworks and I'm trying to get up to speed... The codeigniter tutorial seems well laid out and informative and I know MVC gets lots of praise but I have to say that the general framework/MVC approach sure seems to be very (very) confusing to me. I hope the light comes on soon because I'm just guessing at how this all works?????

#10
[eluser]Unknown[/eluser]
[quote author="Aken" date="1327372880"]What's the URL of the page giving you the 404 error? You can use example.com instead of your live domain.

A couple things you can try:

1) Load the URL helper to your controller constructor, and use the site_url() function to help generate the news slug URL.
Code:
// In News __construct():
$this->load->helper('url');

// Replace your link URL with:
&lt;?php echo site_url('news/' . $news_item['slug']); ?&gt;

2) In your controller's view() method, replace show_404() with something like
Code:
exit('The error is here.');
If your browser then displays the error message instead of CI's 404 page, then you know your model isn't returning the data it's expected to.[/quote]



That's resolves!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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