Welcome Guest, Not a member yet? Register   Sign In
Trailing slash in tutorial
#1

[eluser]fernandoch[/eluser]
I followed the tutorial but in this page http://ellislab.com/codeigniter/user-gui...items.html in the routing part something must be wrong.

If I go to index.php/news it works,

But if there is a trailing slash index.php/news/ then the slug is not properly formed and the url is created as index.php/news/news/slugX for all the links.

Any clues about how to solve this?
#2

[eluser]fernandoch[/eluser]
No one for this?
#3

[eluser]InsiteFX[/eluser]
Show your routes file and the code thats giving you the problem.
#4

[eluser]fernandoch[/eluser]
I am just following the tutorial in this site, the routing is this

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

Taken from here http://ellislab.com/codeigniter/user-gui...items.html at the bottom.

When I go to index.php/news the slug URL is well formed, but if I go to index.php/news/ with a trailing slash, then the slug URL has two news in the URL.

I just followed the tutorial... How to solve it?
#5

[eluser]DarkManX[/eluser]
index.php/news/ would match this one:
Code:
$route[‘news/(:any)’] = ‘news/view/$1’;

index.php/news this one:
Code:
$route[‘news’] = ‘news’;

try to put out the uri you have been led to.
#6

[eluser]InsiteFX[/eluser]
See also:

CodeIgniter Users Guide - URI Class
#7

[eluser]fernandoch[/eluser]
[quote author="DarkManX" date="1343475499"]

try to put out the uri you have been led to.[/quote]

What does that mean?
#8

[eluser]pointmanx[/eluser]
[quote author="DarkManX" date="1343475499"]index.php/news/ would match this one:
Code:
$route[‘news/(:any)’] = ‘news/view/$1’;

index.php/news this one:
Code:
$route[‘news’] = ‘news’;

try to put out the uri you have been led to.[/quote]

@DarkManX: you probably mean output...i.e. something like an echo?

Here's what I did (temporarily) to check the portion of the uri that was getting sent as the slug:

(inside /appplication/controllers/news.php)
Code:
public function view($slug)
{
  echo $slug;

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

It turned out that when following the tutorial routing:
Code:
$route[‘news/create’] = ‘news/create’;
$route[‘news/(:any)’] = ‘news/view/$1’;
$route[‘news’] = ‘news’;
I was getting back the segment pointing to the view function, not the segment pointing to the slug. My solution was to add the 'view' segment into the routing rule:

Code:
$route[‘news/create’] = ‘news/create’;
$route[‘news/view/(:any)’] = ‘news/view/$1’;
$route[‘news’] = ‘news’;

This means that the only way to get to the item identified by the slug segment is to do so directly off of the view function segment. Perhaps this was not what the author intended, but there's no hint in the tutorial to say where
Code:
$1
comes from, so there's no other reasonable assumption than it corresponds with the segments that fall under
Code:
(:any)
which, based on my echo result, gets truncated to only the first segment (removes everything after and including the '/')

Hope this helps to shed some light on the issue.



PS--
@InsiteFX (or whoever is the author of the tutorial):
Please review this and update your tutorial as necessary, for the sake of other new users who, in trying just to get the basics, run across this issue and potentially get frustrated before they have enough understanding of routing to troubleshoot what is going on.
#9

[eluser]Aken[/eluser]
This has been brought up SO many times now.

The URL the tutorial tells you to create is a relative URL. Relative URLs suck. It should recommending using an absolute URL, or even better, the site_url() or anchor() functions from the URL helper.
#10

[eluser]Unknown[/eluser]
[quote author="Aken" date="1345941381"]This has been brought up SO many times now.

The URL the tutorial tells you to create is a relative URL. Relative URLs suck. It should recommending using an absolute URL, or even better, the site_url() or anchor() functions from the URL helper.[/quote]

Thank you Aken. That was the exact problem I was having.




Theme © iAndrew 2016 - Forum software by © MyBB