CodeIgniter Forums
Slug problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Slug problem (/showthread.php?tid=53771)



Slug problem - El Forum - 08-08-2012

[eluser]msy07[/eluser]
Good day!

I just wanted to ask you guys if anyone have encountered this weird problem.

I was trying out the codeigniter tutorial and I was in this specific page:

http://localhost/CI/index.php/news

and that page fetches all the news entries from the database and displays all the news
like a blogroll with each news entry a "read more link" with a url "http://localhost/CI/news/certain_post_here", which is correct.

Now my problem is that when I add up "/" at the end of the url like this:

http://localhost/CI/index.php/news/

the "read more" link changes to this "http://localhost/CI/news/news/certain_post_here" which is not correct because it double's up the url segment of the controller which would lead to a wrong destination.

I hope someone could help me figure this thing out.

Any help would be greatly appreciated.






Slug problem - El Forum - 08-08-2012

[eluser]PhilTem[/eluser]
Please show us your view and how you're creating the links! This way it's easier for us to help you!


Slug problem - El Forum - 08-08-2012

[eluser]CroNiX[/eluser]
Why add the trailing slash anyway? It doesn't provide any benefit unless something comes after.


Slug problem - El Forum - 08-08-2012

[eluser]msy07[/eluser]
Thank you for the replies!

[quote author="PhilTem" date="1344443785"]Please show us your view and how you're creating the links! This way it's easier for us to help you![/quote]

This is the view for showing up all the news.

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 href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p> //this is the line that produces the link to view the specific post

&lt;?php endforeach ?&gt;

[quote author="CroNiX" date="1344446041"]Why add the trailing slash anyway? It doesn’t provide any benefit unless something comes after.[/quote]

I was just trying to figure out why it behaves that way. Smile


Slug problem - El Forum - 08-08-2012

[eluser]srpurdy[/eluser]
[quote author="msy07" date="1344481343"]Thank you for the replies!

[quote author="PhilTem" date="1344443785"]Please show us your view and how you're creating the links! This way it's easier for us to help you![/quote]

This is the view for showing up all the news.

Code:
&lt;?php foreach ($news as $news_item): ?&gt;

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    <p><a href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p> //this is the line that produces the link to view the specific post

&lt;?php endforeach ?&gt;

[quote author="CroNiX" date="1344446041"]Why add the trailing slash anyway? It doesn’t provide any benefit unless something comes after.[/quote]

I was just trying to figure out why it behaves that way. Smile[/quote]

Actually has Nothing to do with codeigniter. That's how paths work.

Change this
Code:
<a href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a>

to This
Code:
<a href="&lt;?php echo base_url();?&gt;news/&lt;?php echo $news_item['slug'];?&gt;">View article</a>

It's always good practice to use base_url or site_url because this saves a lot of problems.


Slug problem - El Forum - 08-08-2012

[eluser]CroNiX[/eluser]
Code:
<a href="&lt;?php echo base_url('news/' . $news_item['slug']);?&gt;">View article</a>
Code:
&lt;?php echo anchor('news/' . $news_item['slug'], 'View Article'); ?&gt;



Slug problem - El Forum - 08-08-2012

[eluser]msy07[/eluser]
[quote author="CroNiX" date="1344488838"]
Code:
<a href="&lt;?php echo base_url('news/' . $news_item['slug']);?&gt;">View article</a>
Code:
&lt;?php echo anchor('news/' . $news_item['slug'], 'View Article'); ?&gt;
[/quote]

Thank you guys for helping me solve my problem! Big Grin


Slug problem - El Forum - 08-08-2012

[eluser]Aken[/eluser]
Use site_url(), not base_url() - that will insure that index.php is added to your URLs if you have not used .htaccess to remove it. Otherwise if you ever need to disable .htaccess, all of your links will be broken.

anchor() uses site_url() already, so that's a good option, too.