Welcome Guest, Not a member yet? Register   Sign In
Rewriting URLs that have variables in
#1

[eluser]Craig Ward[/eluser]
Hi, I have just started using codeigniter and love it. I am rewriting my website with codeigniter and have something I want to do but not sure it is possible.

I have a gallery on my site powered by the Flickr API. Here is an example of the code I use to display the Landscape pictures:
Code:
<?php foreach ($landscapes->photoset->photo as $l->photoset->photo) : ?>
   <a >photoset->photo->farm ?&gt;/&lt;?php echo $l->photoset->photo->server ?&gt;/&lt;?php echo $l->photoset->photo->id ?&gt;/&lt;?php echo $l->photoset->photo->secret ?&gt;/&lt;?php echo $l->photoset->photo->title ?&gt;'>
   <img class='f_thumb'>photoset->photo->farm ?&gt;.static.flickr.com/&lt;?php echo $l->photoset->photo->server ?&gt;/&lt;?php echo $l->photoset->photo->id ?&gt;_&lt;?php echo $l->photoset->photo->secret ?&gt;_s.jpg' title='&lt;?php echo $l->photoset->photo->title ?&gt;' alt='&lt;?php echo $l->photoset->photo->title ?&gt;' /></a>
&lt;?php endforeach; ?&gt;

As you can see when a user clicks on a picture I pass over the Farm, Server, ID, Secret and Title elements using URI segments and build the page in the controller using
Code:
$data['farm'] = $this->uri->segment(3);
$data['server'] = $this->uri->segment(4);
$data['id'] = $this->uri->segment(5);
$data['secret'] = $this->uri->segment(6);
$data['title'] = $this->uri->segment(7);

Everything works and is fine but the URL's are a tad long, example "http://localhost:8888/wip/index.php/gallery/focus/3/2682/4368875046/e8f97f61d9/Old Mill House in Donegal"

Is there a way to rewrite the URL so its more like "http://localhost:8888/wip/index.php/gallery/focus/Old_Mill_House_in_Donegal"

I was looking at using:
Code:
$url_title = $this->uri->segment(7);
$url_title = url_title($url_title, 'underscore', TRUE);

But I don't seem to be able to get it to work. Any ideas?
#2

[eluser]danmontgomery[/eluser]
[quote author="Craig Ward" date="1269531915"]But I don't seem to be able to get it to work.[/quote]

Can you be more specific?
#3

[eluser]Craig Ward[/eluser]
The url is being rewitten. The Codeigniter userguide doesn't have many examples.
#4

[eluser]danmontgomery[/eluser]
I'm still not sure I understand... If you build the link using:

Code:
$url_title = $this->uri->segment(7);
$url_title = url_title($url_title, 'underscore', TRUE);
echo anchor('gallery/focus/' . $url_title);

You would end up with exactly what you're looking for:

Quote:http://localhost:8888/wip/index.php/gall...in_donegal

Assuming title is unique, you could then use that to get the information you need...
#5

[eluser]Craig Ward[/eluser]
The current url is for example:
“http://localhost:8888/wip/index.php/gallery/focus/3/2682/4368875046/e8f97f61d9/Old Mill House in Donegal”

Now rather than just putting in underscores so it is like:
“http://localhost:8888/wip/index.php/gallery/focus/3/2682/4368875046/e8f97f61d9/Old_Mill_House_in_Donegal”

I want to rewrite the URL so it looks like:
“http://localhost:8888/wip/index.php/gallery/focus/Old_Mill_House_in_Donegal”

I don't think I am implementing it correctly or even if that is what url_title() is suposed to do.
#6

[eluser]danmontgomery[/eluser]
It is... url_title() just converts a string to an seo-friendly (and url friendly) string.
#7

[eluser]Craig Ward[/eluser]
Yes but I also want to get rid of the /3/2682/4368875046/e8f97f61d9 uri segments
#8

[eluser]danmontgomery[/eluser]
Code:
foreach ($landscapes->photoset->photo as $photo) {
    $img_data = array(
        'class' => 'f_thumb',
        'src' => $photo->farm . '.static.flickr.com/' . $photo->server . '/' . $photo->id . '_' . $photo->secret . '_s.jpg',
        'title' => $photo->title,
        'alt' => $photo->title
    );
    echo anchor('gallery/focus/' . url_title($photo->title), img($img_data));
}

in config/routes.php,
Code:
$route['gallery/focus/(:any)'] = 'gallery_controller/gallery_method/$1';

And the function to show the image:

Code:
function gallery_method($url_title) {
    $this->load->model('gallery');
    $img = $this->gallery->get_by_url_title($url_title);
    // ... etc
}
#9

[eluser]Craig Ward[/eluser]
Cheers for that, I will give this whirl later tonight and let you know how I get one.

Thanks for the help Smile




Theme © iAndrew 2016 - Forum software by © MyBB