Welcome Guest, Not a member yet? Register   Sign In
Tutorial error...?? (RESOLVED)
#11

[eluser]Benair[/eluser]
I'm sure I'll get blasted for this and I'm not going to exhaust the issue beyond this, but with all due respect to those who already know what they are doing here, it didn't help to explain why it isn't working to us who are totally green to MVC and OOP compared to NVP linking and who are trying to baby-step through the early tutorial phase of learning it.

I just went back and re-did the steps provided (and added some notes to the best of my interpretation .

Code:
// In News __construct():
$this->load->helper('url');
so Add that line shown above to controllers/news.php - I assume under the
Code:
$this->load->model('news_model');

(then go to news/views/index.php where the link url is)
Code:
<p><a >View article</a></p>
AND
Code:
&lt;!-- // Replace your link URL with: --&gt;
&lt;?php echo site_url('news/' . $news_item['slug']); ?&gt;

(then back in controllers/news because that is where the show 404 is located)
Code:
if(empty($data['news_item']))
{
  //show_404();
  exit('The error is here.');
}

so now in place of my original links that did display (however resulted in all not founds when clicked before)now it returns this plain list of full URLs unlinked.
...mysite.com/code_igniter/index.php/news/slug1
...mysite.com/code_igniter/index.php/news/slug2
...mysite.com/code_igniter/index.php/news/slug3


the list of urls look ok to me... but is that supposed to clue me as to why it isn't linking to results?

In any event I'm moving on from that issue. I appreciate you and anyone taking the time to help others but sometimes it needs to be spelled out a little more for newbe's and assume it doesn't make perfect sense to everyone especially those looking for a "tutorial".



#12

[eluser]jamiejonks[/eluser]
I have just encountered a similar problem. Shame these comments can’t be shown underneath the tutorial rather than hidden away in a forum. Anyway, this is what I found:

1) The view (views/news/index.php) should contain a simpler href – just the PHP bit without ’news/‘ at the beginning.

2) if when creating the database records you used spaces in your slug field, you’ll get the 404 error from your new controller. I thought the purpose of the slug field was for a subheading to the news article, so of course my examples did contain spaces. The solution was to first encode the slug in the view (views/news/index.php):
Code:
<p>a href="&lt;?php echo rawurlencode($news_item['slug']) ?&gt;">View article</a></p>
(Actually, that step isn’t strictly necessary because the browser will do it automatically, but it makes the final HTML follow the spec)
NB I’ve left the opening angle bracket off the a tag here, because the forum wouldn’t let me include links in my post.

Then, in the news controller, add the line
Code:
$slug = rawurldecode($slug);
or similar to the view() function (method). It will now work.

I don’t know if this is the best way to do it.
#13

[eluser]Aken[/eluser]
1) You'd need to make a custom helper then - because if you don't include the "news/" part, you're not linking to the proper place. And creating a custom function to keep yourself from needing to write five characters seems a little overkill, no?

2) A slug is a chunk of the URL associated with news posts. It should not have spaces in it in the first place. If you added your fields directly to the database and included spaces, then you're doing it improperly. Not that it's entirely your fault - the tutorial is not overly explanatory about various aspects (it should describe what a slug actually is). But the code you propose is unnecessary, as the model's set_news() function will create a slug appropriately based off the news item's title.
#14

[eluser]CroNiX[/eluser]
Yes, they should have just used the url_title() function from the URL Helper to create the slugs.
Edit: I guess they do.
#15

[eluser]jamiejonks[/eluser]
If you have a page at index.php/news with links on it to news/slugname, as you will if you follow the news tutorial, you end up with links in the form of index.php/news/news/slugname, i.e. with two 'news' parts. Using the site_url() function or not typing 'news' into the href solves this, so the route given in the tutorial ($route['news/(:any)'] = 'news/view/$1'Wink works. I can't see how this modification is me 'not linking to the proper place', but rather that the tutorial contains an error.

Thanks for clarifying what the slug is, it's a shame the tutorial doesn't tell you not to put spaces in when manually creating a few records. Having said that, I'm sure at some point it would still be useful to know how to handle spaces (and anything 'unusual') in URLs. I haven't found a definitive 'best practice' answer to this.
#16

[eluser]Aken[/eluser]
My comment assumes that you'd be using the site_url() function. If you don't include the "news/" portion inside the site_url() function, your URLs will not be accurate. If you're using relative URLs without the site_url() function, then no, you would not need the "news/" portion in your link. But relative URLs can be easily broken if not controlled properly, hence our recommendation to use site_url(). Perhaps the tutorial does have a small error - I didn't probe through it enough. But it is a bit out of date and could use a revamp. It might even have been revamped already in preparation for the next CI release.
#17

[eluser]Unknown[/eluser]
I hit this same issue this evening going through the tutorial. For me the issue was located here:
Code:
public function view($slug)
  {
   $data['news'] = $this->news_model->get_news($slug);
   if (empty($data['news_item']))
   {
    show_404();
   }

I was populating $data['news'] but the rest of the function is looking at $data['news_item']. Changing those references to $data['news'] resolved the issue.

The tutorial uses $data['news_item'] for that function so it's unclear to me if some framework magic is supposed to be happening and isn't or it's a misprint in the tutorial.
#18

[eluser]ivanhalen[/eluser]
[quote author="jamiejonks" date="1329422846"]If you have a page at index.php/news with links on it to news/slugname, as you will if you follow the news tutorial, you end up with links in the form of index.php/news/news/slugname, i.e. with two 'news' parts. Using the site_url() function or not typing 'news' into the href solves this, so the route given in the tutorial ($route['news/(:any)'] = 'news/view/$1'Wink works. I can't see how this modification is me 'not linking to the proper place', but rather that the tutorial contains an error.

Thanks for clarifying what the slug is, it's a shame the tutorial doesn't tell you not to put spaces in when manually creating a few records. Having said that, I'm sure at some point it would still be useful to know how to handle spaces (and anything 'unusual') in URLs. I haven't found a definitive 'best practice' answer to this.[/quote]

I just couldn't say better :-)
Yes, following the tutorial as-it-is leads to double "news" part in the url (index.php/news/news/slugname), that is a real pain for novices trying to approach MVC and wondering "where am I wrong? Am I so bad in PHP that I can't follow the most basic tutorial?"... (and yes, the "site_url" stuff solves the problem... and yes, I banged my head too, before realizing I wasn't the problem)

CI is well known for its documentation, among the other advantages: please, CI Masters, don't let users run away from this wonderful framework back to procedural programming just because the introducing tutorial could be written better! Fix it ASAP! Thanks
#19

[eluser]Peter Englmaier[/eluser]
As a Newbee I did the news-tutorial of course and went straight into the 404-error (August, 21, 2012). The issue with $news_item and $news was the problem. Thank you, "lesharris".

Here in detail for the beginners what has to be changed in the code:

In ".../views/news/view.php": change $news_item to $news (two variables)

In ".../views/news/index.php: change $news_item to $news (four variables)

In ".../controller/news.php":
change old part of code:
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');
}

to the NEW part of code:
Code:
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);

if (empty($data['news']))
{
  show_404();
}

$data['title'] = $data['news']['title'];

$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}

Then it works!
Don't forget to reload the pages (the cache!) ... as I did forget ;-)
#20

[eluser]Peter Englmaier[/eluser]
... und hier das ganze auf deutsch:

Beim Durcharbeiten des Tutorials kam ich natürlich auch auf den 404-Fehler bei der Nachrichten-Ansicht.

Das Problem lag an den Variablen-Definitionen mit $news_item und $news. Hier im Detail, was geändert werden muss:

In ".../views/news/view.php": ändere $news_item in $news (2 x)

In ".../views/news/index.php: ändere $news_item in $news (4 x)

In ".../controller/news.php":
ändere den ALTEN Code-Teil:
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');
}

in den NEUEN Code-Teil:
Code:
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);

if (empty($data['news']))
{
  show_404();
}

$data['title'] = $data['news']['title'];

$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}

Dann klappt's!
Ach so, nicht vergessen: Seiten neu laden (Cache!).




Theme © iAndrew 2016 - Forum software by © MyBB