Welcome Guest, Not a member yet? Register   Sign In
Tutorial - I can not view the news (view/test-news)
#1

[eluser]thebigpj[/eluser]
I have followed the tutorial exactly but I am faced with one small problem - Viewing the news articles themselves. Or did the tutorial stop before this point?

index.php/news/ - This works.

index.php/news/create - This works.

index.php/news/test-news - This does not display my article.
index.php/news/view/test-news - This does not display my article.
index.php/news/view/view/test-news - This does not display my article.

I have tried the above with and without the routing (config/routes.php) changed.

Or is this beyond the scope of this tutorial?

Many Thanks
#2

[eluser]stuartr[/eluser]
Having just worked through the tutorial there are some issues in following it as laid out.

When you try to view the news article what is the URL that is generated?
#3

[eluser]TheFuzzy0ne[/eluser]
From what I can see, there is the following method for viewing entries:
Code:
public function view($slug)
{
    $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');
}

If you're getting a 404 error, that's most likely due to one of two things.

1) You're not using the correct slug (or the article doesn't exist in the database).
2) Your view method is not in the right place (it might be in your model instead of your controller).
#4

[eluser]stuartr[/eluser]
[quote author="TheFuzzy0ne" date="1363686817"]
If you're getting a 404 error, that's most likely due to one of two things.

1) You're not using the correct slug (or the article doesn't exist in the database).
2) Your view method is not in the right place (it might be in your model instead of your controller).[/quote]

There is a third option, which has happened to a number of people, including myself - if it is the cause of the issue it will become apparent in the URL that is generated for the news article. The tutorial uses a relative URL, and this can lead to the URL generated including the news segment twice e.g. /news/news/

If this is the case, then the link in the view file should be amended to use a static URL possibly using the URL helper function as below

Code:
$view_segments=array('news',$news_item['slug']);
....
Code:
<a href="&lt;?php echo site_url($view_segments);?&gt;">View article</a>
#5

[eluser]thebigpj[/eluser]
This was the correct url I should be using: index.php/news/test-news

TheFuzzy0ne: Thanks. My first issue was that $data['news'] was meant to be $data['news_item'].

stuartr: Thanks. Yes the urls did look off which is why I was unsure which url was correct.

This was a cross of two problems: First my own mistake for making a typo and the link url to the news article themselves. It was coming up news/news/ etc... I changed it personally to
Code:
"./&lt;?php echo $news_item['slug'] ?&gt;"

Your solution of site_url() also works. Which should I use?

Many Thanks
#6

[eluser]stuartr[/eluser]
Glad that helped - I went through much the same issues as you did.

Personally I would go for the site_url approach as regardless of where you include the call the link from (adding sub pages etc.) it should work. Relative URLs are always going to be prone to errors if the structure of a site is amended, and therefore I personally believe its a good habit to use static URLs built with the use of the helper.

#7

[eluser]Unknown[/eluser]
I had the issue with the doubling up of news in the url to the single article.
ex. index.php/news/news/FirstNewsTest when it should have been index.php/news/FirstNewsTest

The problem for me was simple. The main news page should be index.php/news and somehow I ended up with an extra / at the end so I was viewing index.php/news/

If your looking at index.php/news and the relative url is news/<article> then everything is fine
If your looking at index.php/news/ and the relative url is news/<article> then your link is going to point to index.php/news/news/<article> which of course wont work.

Not sure this will help anyone but I thought I would share.

I also agree that Aken was being needlessly annoying. He looks smug in his picture though so that is probably to be expected. Maybe instead of trying to intentionally anger people while pretending your really trying to help...you could really try to help.




Theme © iAndrew 2016 - Forum software by © MyBB