CodeIgniter: URIs and Forms

2020-06-28 16:37发布

I'm implementing a search box using CodeIgniter, but I'm not sure about how I should pass the search parameters through. I have three parameters: the search string; product category; and the sort order. They're all optional. Currently, I'm sending the parameters through $_POST to a temporary method, which forwards the parameters to the regular URI form. This works fine. I'm using a weird URI format though:

http://site.com/products/search=computer,sort=price,cat=laptop

Does anyone have a better/cleaner format of passing stuff through? I was thinking of passing it into the products method as arguments, but since the parameters are optional things would get messy. Should I suck it up, and just turn $_GET methods on? Thanks in advance!

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-06-28 16:44

A much better approach, and the method the CI developers intended, is to add all your search parameters to the URI instead of a query string like so:

http://site.com/products/search/term/computer/sort/price/cat/laptop

You would then parse all the URI segments from the 3rd segment ("term") forward into an array of key => value pairs with the uri_to_assoc($segment) function from the URI Class.

Class Products extends Controller {
...

    // From your code I assume you are calling a search method.
    function search()
    {
        // Get search parameters from URI.
        // URI Class is initialized by the system automatically.
        $data->search_params = $this->uri->uri_to_assoc(3);
        ...
    }
    ...
}

This would give you easy access to all the search parameters and they could be in any order in the URI, just like a traditional query string.

$data->search_params would now contain an array of your URI segments:

Array
(
    [term] => computer
    [sort] => price
    [cat] => laptop
)

Read more about the URI Class here: http://codeigniter.com/user_guide/libraries/uri.html

查看更多
神经病院院长
3楼-- · 2020-06-28 16:59

Query Strings

You can enable query strings in CodeIgniter to allow a more standard search function.

Config.php

$config['enable_query_strings'] = FALSE;

Once enabled, you can accept the following in your app:

http://site.com/products/search?term=computer&sort=price&cat=laptop

The benefit here is that the user will find it easy to edit the URL to make a quick change to their search, and your search uses common search functionality.

The down side of this approach is that you are going against one of the design decisions of the CodeIgniter development team. However, my personal opinion is that this is OK provided that query strings are not used for the bulk of your content, only for special cases such as search queries.

查看更多
放荡不羁爱自由
4楼-- · 2020-06-28 17:05

If you're using a fixed number of parameters, you can assign a default value to them and send it instead of not sending the parameter at all. For instance

 http://site.com/products/search/all/somevalue/all

Next, in the controller you can ignore the parameter if (parameter == 'all'.)

 Class Products extends Controller {
 ...

     // From your code I assume that this your structure.
     function index ($search = 'all', $sort = 'price', $cat = 'all')
     {
         if ('all' == $search)
         {
            // don't use this parameter
         }
         // or
         if ('all' != $cat)
         {
            // use this parameter
         }
         ...
     }
     ...
 }
查看更多
登录 后发表回答