Codeigniter “News section” tutorial issue

2019-06-06 00:37发布

问题:

I have a problem with the URI. I've tried to look through the post, but couldn't see the answer.

Tutorial can be found here: http://codeigniter.com/user_guide/tutorial/news_section.html

Basically I replicated all and it works fine, but there one think where I don't know whether is my browser or I miss-configured something.

In the "views/news/index.php" I have the following code as per tutorial:

<h2><? echo $news_item['title'] ?></h2>
<div id="main">
    <? echo $news_item['text']?>
</div>
<p>[b]<a href="news/<? echo $news_item['slug']?>">View Article</a>[/b]</p>

Now the problem is that when I look into the source html on my browser I see the link as:

View Article

Which I assume is correct. But when I click on, it points me to:

localhost/CI/index.php/news/news/firstnews

As you can see the "news" is repeated twice in the URI. It looks like the browser is simply appending the href value to the URI that is open, instead of cleaning it down to index.php and add it there.

I tried to remove the "news" bit from the href value and it works fine.

I am using Chome if that makes any difference.

Is that my misstake? Or this is just an error in the tutorial?

回答1:

Igor, I think, that's a tutorial's mistake and you need add / in the href attribute. E.g.

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

Besides, you should use URL Helper - http://codeigniter.com/user_guide/helpers/url_helper.html

P.s. there is a chance that you don't tune .htaccess file.



回答2:

In order to avoid this kind of mistakes I often use base_url() when writing down the links. Like this:

<?php echo anchor(base_url('news/'.$news_item['slug']),View article); ?>

Note that I use the the anchor() & base_url() functions of the URL helper. More info at:

http://codeigniter.com/user_guide/helpers/url_helper.html



回答3:

This is a mistake in the CI tutorial.


First of all it's recommended to use the site_url() function to help generate the news slug URL.

You do this by adding $this->load->helper('url'); to the __construct() of your News controller in controllers/news.php

Then change your link URL in your views/news/index.php to <?php echo site_url('news/' . $news_item['slug']); ?>


The most crucial mistake is that throughout your current files you're populating $data['news'] while the rest of your functions are looking at $data[‘news_item’]

You should change these (two) variables in views/news/view.php and the (three) variables in the view() function in controller/news.php

After reloading the page, that should do it! For the full discussion on the CI forums check the following link: http://ellislab.com/forums/viewthread/209349/



回答4:

For me i changed

   $data['news'] = $this->news_model->get_news($slug);

to:

   $data['news_item'] = $this->news_model->get_news($slug);

and that works fine, now



回答5:

BTW, I found this git repo github.com/Crias/tutorial-codeigniter-news which contains source code for the default codeigniter tutorial.

Now you don't need to type (or copy-paste) it again to make the tutorial works.