0

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,

1

3 Answers 3

1

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~%.:_\-';
Sign up to request clarification or add additional context in comments.

1 Comment

Oh that first bit makes alot of sense now, i guess i just have to have alot of if else statements to point people in the right directions, thanks
0

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,

Comments

0
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.