CodeIgniter Pagination & GET - codeigniter-2

See: http://codeigniter.com/user_guide/libraries/pagination.html
I do not want to use URI segments for my pagination (as I have a lot of possible parameters to pass in unknown order). Currently, I have $config['enable_query_strings'] = FALSE;, and if I set it to TRUE, I get something like this (from CI's example):
http://example.com/index.php?c=test&m=page&per_page=20
Where "c" is the controller, "m" is the method, etc. This is not what I want.
I want something like this:
http://domain.com/?min_amount=100&max_amount=200&color=blue&size=large&limit=20&limit_offset=100
Is there a way to achieve this in CI? If not, is there some library already available that I can use?

In CodeIgniter, there are two configuration options when it comes to query strings.
One option ($config['allow_get_array'], enabled by default) allows you to pass additional queries on to the already segmented URI, which would then be available via the $this->input->get() controller library function. You would set the pagination config using this function, allowing you to separate the route, and the pagination instructions.
The other option ($config['enable_query_strings']) forces you to use the query strings in place of the segmented URIs.

Related

Calling a controller/action through query parameters

Is it possible to call a controller and action directly by using query parameters in Laravel? I see some frameworks allow /index.php?_controller=X&_action=Y, or Yii allows /index.php?r=X/Y. I was wondering if something similar was possible in Laravel/Symfony.
symfony
The query string of a URL is not considered when matching routes. In this example, URLs like /blog?foo=bar and /blog?foo=bar&bar=foo will also match the blog_list route.
https://symfony.com/doc/6.0/routing.html
laravel afaik doesnt support that either
you are obviously free to just forward yourself
e.g. write one router that forwards to other controllers
symfony https://symfony.com/doc/6.1/controller/forwarding.html

Enable query string access only to select few Controller functions?

In few pages I need to make pagination. This of course can be achieved with URI segments, but in few cases in addition to pagination parameters, I need to pass some other GET parameters for filtering purposes.
So obviously in this case i need to be able access the controller via query string like so:
example.com/?c=controller&m=function
In order to achieve this I set the enable_query_strings to TRUE in main config file.
This seemed to work, but I discovered that it breaks a bunch of different stuff. For example if I use current_url() the URL returned has a ? at the end to accommodate the query string. So if I use it in form, it does not work.
So is there any way to enable the controller access to controller functions only to specified functions?
You can construct you url like this:
/param1/param2/pagination_parameters
So you will be able to send custom data (number of params), and pagination data using just URI segments.
For example if I use current_url() the URL returned has a ? at the end
to accommodate the query string. So if I use it in form, it does not
work.
Please note, that you can also left form 'action' blank, so result will be the same as if you had used current_url() (http request will go to the same script).
Instead of enabling enable_query_strings in main config, just enable in pagination config only. So it will apply to that particular page only.
Ex:
$config['page_query_string'] = TRUE;

Ajax results filtering and URL parameters

I am building a results filtering page using AJAX requests. I would like to reflect the filters in the URL. For example: for price_from I want to add ?price_from=VAL to the URL.
I have a backend that is capable of rendering the page with URL parameters.
After some googling I would a Backbone.router solution which has a hash fallback for the IE that does not support HTML5 history API.
I have a problem with setting a good philosophy of routes. I have a set of filtering parameters (price_from, price_to, color, ...) and I would like to attach each parameter to one route.
Is that possible to chain the routes to match for example: ?price_from=0&price_to=1&color=red? (the item order can change)
It means: call all the routes at the same time and keep the ie backwards compatibility?
Your best bet would be to have a query portion of the URL rather than using GET parameters to denote the search criteria. For example:
Push state: /search/query/price_from=0&price_to=1&color=red
Hash based: #search/query/price_from=0&price_to=1&color=red
Your backend would of course need to change a bit to be able to parse the new URL structure.

Codeigniter and Pagination with Query Strings

I am trying to build a Search with Pagination in Codeigniter and would love some help with it.
So far, I've realized that I can not use BOTH url segments and query strings together. Using only query strings produces very ugly URLs.
I understand that Codeigniter destroys the GET and I'm trying to put it back in. Ergo... if I place this in the constructor of the search controller, will my problems be solved?
parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
As in, if it works for me, is there anything I need to be aware of security wise?
So far, I've realized that I can not use BOTH url segments and query strings together.
Sure you can. Try this in your config:
$config['uri_protocol'] = "PATH_INFO";
That should get things started. Now, since CI abandons and empties the $_GET variable, you need to repopulate it like this:
parse_str($_SERVER['QUERY_STRING'],$_GET);
Now the only real concern here is that, if you have global XSS filtering on, you should know that you just manually parsed the query string into the global $_GET variable. This means you haven't passed it through any XSS filters. In CI 1.x you can access the filter through the input library like this:
$myvar = $this->input->xss_clean($_GET['myvar']);
In CI 2.x you do it through the security library like this:
$myvar = $this->security->xss_clean($_GET['myvar']);
Of course, it goes without saying that you can extend the Controller class to have a get() method that does all this automatically such that you can do this:
$myvar = $this->get('myvar');

Should I still use querystrings for page number, etc, when using ASP.NET 4 URL Routing?

I switched from Intelligencia's UrlRewriter to the new web forms routing in ASP.NET 4.0. I have it working great for basic pages, however, in my e-commerce site, when browsing category pages, I previously used querystrings that were built into my pager control to control paging and now am not sure how to handle this using routing.
I defined a MapPageRoute as:
routes.MapPageRoute("cat-browse", "Category/{name}_{id}", ~/CategoryPage.aspx");
This works great. Now, somebody clicks to go to page 2. Previously I would have just tacked on ?page=2 to the url. How do I handle this using web forms routing? I know I can do something like:
http://www.mysite.com/Category/Arts-and-Crafts_17/page/2
But in addition to page, I can have filters, age ranges, gender, etc.
Should I just keep defining routes
that handle these variables like
above?
Should I continue using querystrings
and if so, how do you define a route
to handle that?
The main reason to use url routing is to expose clean, user-and-SEO-friendly, URLs. If this is your goal, then try to stick to it and not use querystring parameters. Note: I don't believe we need to completely ban the use of querystrings and, depending on your situation, you may decide it best to use querystring parameters for parameters that are not used frequently, or where no real value is added by making the information more semantically meaningful.
So here's what I would do:
Define a catch-all for all your other parameters:
routes.MapPageRoute("cat-browse", "Category/{name}_{id}/{*queryvalues}", "~/CategoryPage.aspx");
In /CategoryPage.aspx, access the router parameter and then parse as appropriate:
Page.RouteData.Values["queryvalues"]
Instead of using the syntax of Arts-and-Crafts_17/**page/2/age/34** for these parameters, I perfer to use the following syntax: Arts-and-Crafts_17/pg-2/age-34/
If you do this, the catch-all parameter 'querystring', will equal pg-2/age-34. You can now easily parse this data and add each name/value to the page context. Note that you will need to do something along these lines since each of these parameters are optional on your site.
You can take advantage of C# 4.0 named and optional parameters. Please have a look at this example from haacked
If you are using a lower version of the framework, you can also use code from the link above. But instead of declaring the method as
public ActionResult Search(int? page=0)
{}
you can declare it as
public ActionResult Search(int? page)
{
if(page == null)
{
page=0;
}
}
HTH

Resources