Welcome Guest, Not a member yet? Register   Sign In
User's Guide Tutorial - News Section - Error
#1

[eluser]Unknown[/eluser]
May have found a small error in the news section user's guide on the following page:
http://ellislab.com/codeigniter/user-gui...ction.html

For index.php, it creates a link using the code:
href="news/<?php echo $news_item['slug']

This provides a link of ".../news/news/slug..." causing a breakdown of the link to give a 404 page error since the slug being passed to the database is "news". Instead, removal of the "news/" resolved it for my situation. Can anyone confirm?
href="<?php echo $news_item['slug']

#2

[eluser]silviateona[/eluser]
Hi nwolybug,

I'd say you're right !!!
Thank you so much for posting this.

Going through the tutorial, I came across this bug as well. Your solution works, so let's hope that the CodeIgniter editors see it and fix the bug. Have a great day!

#3

[eluser]Tim Brownlaw[/eluser]
Well as the tutorial (and the code) stands, it is correct... with a little trap!
I couldn't see off the bat, why this wasn't working so I took a good hard look at it.

Here is what I found.... It breaks with a trailing slash as shown below... Note: wombats was my slug Smile
Incorrect!
Code:
http://cidev22.com/index.php/news/
Note: The Trailing Slash and the links will be of the form
Code:
http://cidev22.com/index.php/news/news/wombats

Correct!
IF you followed the tutorial which does state to use
Code:
http://cidev22.com/index.php/news
Note: NO Trailing Slash , the link will be of the form
Code:
http://cidev22.com/index.php/news/wombats
which is the desired url!

The reason why is the way the links are built...
To avoid this situation in your own code... It's imperative that you don't leave this kind of thing to chance...
But remember that this is a simple tutorial!

To avoid this requires a few more steps...

Specify the full URL. So in your application/views/news/index.php, change it to...
Code:
<?php
foreach ($news as $news_item): ?>
<h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
<div class="main">
&lt;?php echo $news_item['text'] ?&gt;
</div>
<p><a href="&lt;?php echo base_url('index.php/news/'. $news_item['slug'] )?&gt;">View article</a></p>
&lt;?php endforeach ?&gt;

This is a little yucky as it requires the inclusion of index.php in the link... until you make the changes to remove index.php in your urls...

So to make base_url() available you need to add the url helper into your news.php controller

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

That is not the definitive way to do things... it's "a way"! You have the flexibility to do this many ways to achieve the same result as you'll learn as you get further into PHP and CI.

So as the instructions stand in the news tutorial, they are correct... IF followed to the letter!

So you can break it... Remember that most users will be able to break things if you give them half the chance!
So this makes this a rather good tutorial!
#4

[eluser]silviateona[/eluser]
Hi Tim,

Thanks for your helpful input :-) Your suggestion to indicate a base url of "index.php/news/" solved the perceived bug as well.

I see now that, as you point out, the bug was indeed not in the tutorial but, as far as I can guess, most likely got introduced in my own installation when I used "mod-rewrite" to omit "index.php" from all CI urls, as per another tutorial's suggestion.

All good now :coolsmile:
#5

[eluser]praveens[/eluser]
Hi Tim,

Thank You!




Theme © iAndrew 2016 - Forum software by © MyBB