views:

663

answers:

3

Ive recently started using CodeIgniter as I was searching for a very lightweight framework and it seemed to come up as a top choice.

I am new to the whole MVC thing, enjoying it but am stuck with something which seems very simple.

I am coding a CMS and need a way to filter, sort and paginate the results. I am used to doing it with querystrings, so I would have something along the lines of:

articles.php?order=title&sort=desc&filter=articletitle&page=5

I have no idea how I would go about doing this in CI so I just turned on the EnableQueryStrings in the config and it works fine, but i am getting the feeling its probably not the most elegant solution.

I suppose I could have

index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5

but to me this seems very inflexible, what if for example i dont need to sort, how would i make sure i am looking at the correct uri segment?

any ideas?

+1  A: 

Have you tried implementing the _remap function? It will redirect all queries to the controller to that function allowing you to implement as many (or as few) of those as you like.

You could then do something like this:

class Articles extends Controller
{
    // Note: No need for a "order" or even an "index" function!
    public function _remap()
    {
        $assoc = $this->uri->uri_to_assoc( 4 );
        /* $assoc is now 
           array( "order"=>"title",
                  "sort"=>"desc",
                  "filter"=>"articletitle", 
                  "page"=>5); */
    }
}
Christopher W. Allen-Poole
A: 

I don't like the fact that CodeIgniter destroys the query string. The query string is great for optional parameters. Trying to put optional parameters in URI segments and things start to get weird.

A URL like this seems a bit of a hack :

index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5

For this reason I configure CodeIgniter to use a mixture of URI segments and query string. This answer shows you how you can achieve this.

Stephen Curran
A: 

It is a bit awkward that you can't use query strings by default. I've overcome this in my current project in a few areas by send the sort preference in a form post and then storing that preference in the user's session.

someoneinomaha