BigJobbies
BigJobbies

Reputation: 4055

CodeIgniter - Dynamic URL segments

I was wondering if someone could help me out.

Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.

As per the CI userguide the uri is built as follows

www.application.com/CLASS/METHOD/ARGUMENTS

This is fine except i need to structure that part a bit different.

In my forum i have categories and posts, so to view a category the following url is used

www.application.com/forums

This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be

www.application.com/forums/mycategory/this-is-my-first-post

I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.

If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.

Cheers,

Upvotes: 0

Views: 2682

Answers (3)

Siddharth Shukla
Siddharth Shukla

Reputation: 1131

Set in within config/routes.php 

$route['song-album/(:any)/:num'] = 'Home/song_album/$id';

fetch in function  with help of uri segment.

$this->uri->segment(1);

Upvotes: 0

Rooneyl
Rooneyl

Reputation: 7902

You could make a route that forwards to a lookup function.
For example in your routes.php add a line something like;

$route['product/(:any)/(:any)'] = "forums/cat_lookup/$1/$2";

This function would then do a database lookup to find the category.

...
public function cat_lookup($cat, $post) {
    $catid = $this->forum_model->get_by_name($cat);
    if ($catid == FALSE) {
        redirect('/home');
    }
    $post_id = $this->post_model->get_by_name($post);
    /* whatever else you want */

    // then call the function you want or load the view
    $this->load->view('show_post');
}
...

This method will keep the url looking as you want and handle any problems if the category does not exist.
Don't forget you can store the category/posts in your database using underscores and use the uri_title() function to make them pretty,

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction

<a href="www.application.com/forums/category/mycategory/this-is-my-first-post">First Post</a>

In the controller you can easily get this

$category = $this->uri->segment(3);
$post     = $this->uri->segment(4);

And now you can proceed. If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.

Go to system/core/uri.php and add this method

function assing_segment($n,$num)
{
    $this->segments[$n] =   $num;
    return $this->segments[$n];
}

How to use

$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');

And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

Upvotes: 1

Related Questions