Trouble with Codeigniter Routes involving a query - codeigniter

I'm having a little trouble with a CodeIgniter route when there is a query (stuff after the ?) in the URI. I know it is good practice to replace queries with routes in CI, but I'm importing in a premade messageboard that already does everything with queries. This is my route:
$route['messageboard/:any'] = "messageboard/index";
Any in this case refers to a script name. So if it's messageboard/admin.php, I have it load a view that loads my premade messageboard's script "admin.php". It's working just fine if I do messageboard/admin.php. It does fine if I do messageboard/admin.php?. If I put a parameter into the query, however, the route won't correctly send the user to the messageboard controller, and instead sends them to a 404. Does anyone have any ideas on how to make this work? I would be eternally grateful. Thanks!

Okay guys, I solved it. I needed to change three things. The first was mtvee's suggestion, which lets it read query strings. The second one you're going to want to change the $config['permitted_uri_chars'] in the config file to include an equals sign, since it starts off disabled and all query strings will be of the for ?a=34 or something like that. The third is you need to go to $config['uri_protocol'] and change it from AUTO to PATH_INFO. Once I did those, it worked.

I'm sure the syntax is:
$route['messageboard/(:any)'] = "messageboard/index"; //<-- notice brackets
and not
$route['messageboard/:any'] = "messageboard/index";

I believe CI doesn't do GET out of the box. Check out Enabling Query Strings here http://ellislab.com/codeigniter/user-guide/general/urls.html

Related

How to remove parameters from url in laravel?

I am trying to remove the url parameters based on some conditions ,i can able to remove some of the parameters , i want to replace the some new value for the url, can you please give me some idea how to do this one
$request->query->remove('key');
$request->query->add(['admin'=>"true"]);
$request->request->remove('key');
$request->request->add(['admin'=>"true"]);
You almost done in a correct way, simply change the order of the queries then it will be working fine.
$request->query->remove('key');
$request->request->remove('key');

How to run single controller method from any route in Laravel 4

I really hope my question has been well thought out but here goes.
How do you implement something like
Route::get("/url1", "controller#method");
Route::get("hello/url1", "controller#method");
Route::get("hello/hi/url1", "controller#method");
in Laravel but using something like
Route::get("*/url1", "controller#method");
instead of declaring every route path?
I will explain why this problem has come up. You see the primary url is always changing because its being called from a js file via a location.href call. I could decide to use a primary url variable but its to be deployed via intranet to different servers in organizations and the primary url could change at any time meaning that localhost/project on one system might become localhost:7987/project on another thus breaking the url variable, now thats on one part. On the other hand there are js functions running continuously and when someone navigates to a deeper url, say from localhost/home to localhost/home/event a route call that should be independent of folder breaks
So yeah, I am wondering if theres a way to declare a global route that points to a controller and/or if this is possible in Laravel.
Thanks
Try this:
Route::get('{something}/url1', 'controller#method')->where('something', '*');
Not sure if that will work, but the idea is that you can use where to pass some Regexp to match selected value from route.

Getting $_GET data in codeigniter while using REQUEST_URI

I am using CodeIgniter. In my script, I am changing $config['index_page'] in config.php file as per the user's reponse, i.e, dynamic index_page is used. In order to get it work, I have changed the $config['uri_protocol'] value to "AUTO".
Everything is working fine except when the case comes like : domain.com/index_page/auth/register?testvar=1
It's not accepting the get variables and "PAGE NOT FOUND" error is there. I have tried several things already discussed here, but they involve changing the uri_protocol to "PATH_INFO" that I can't change as the site stops working. It requires "REQUEST_URI" to work properly which is exactly the case with "AUTO" setting.
So is there, any way to get it working???
Any help would be appreciated.
Have a look at this answer: Handling question mark in url in codeigniter it will require you to override the core URI class whenever you are accepting QUERY_STRING and inject your logic there.
Have you read this: http://codeigniter.com/user_guide/general/urls.html ?
You pass vars like this:
domain.com/index_page/auth/register/1
The first segment represents the
controller class that should be
invoked.
The second segment represents
the class function, or method, that
should be called.
The third, and any
additional segments, represent the ID
and any variables that will be passed
to the controller.
Also you can do it your way, read the reference. But than why to use a framework at all?
I've done the same thing in my project and I got the url like
mydomain.com/search/?name=Arun+David&age=23
To achieve this,
In config file set
$config['uri_protocol']='PATH_INFO'; or $config['uri_protocol']='ORIG_PATH_INFO';
if PATH_INFO is not working try using ORIG_PATH_INFO. For me in localhost PATH_INFO is woking but not ORIG_PATH_INFO but while uploading it in server ORIG_PATH_INFO in working but not PATH_INFO.
and in your search controller's constructer add
parse_str($_SERVER['QUERY_STRING'],$_GET);
then you can use $_GET['name'] and $_GET['age'] in your code!.!. :-)

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

Using and hiding default class

This is my first time getting my hands dirty with CI so I'm getting a little confused.
I'm wanting to accomplish a couple things with my question. First of all, I'd like to always use the default controller without having it to appear in the url. For example, I created a new class named after my site (Example.php) and that works fine. However, if I want to call the search function in my controller I then have to go to example.com/index.php/example/search/.
The second thing I want to accomplish is when I run a search I'll get a nice looking url like so: example.com/search/This+is+a+search (I haven't gotten to removing the index.php portion but I know to use a htaccess). I'm not worried about the actual mechanics of the search, just that I'd like to format the url in this way.
I originally experimented with using a Search class but that found that it doesn't allow me put the search in the url because the second parameter should be a function and not the extra stuff.
Thanks for any help.
In application/config/routes.php file add $route to redirect everything to your controller.
Something like this:
$route['([^\/]+)'] = 'content/index/$1';
$route['([^\/]+)\/([^\/]+)'] = 'content/index/$1/$2';
This will redirect urls like example.com/A and example.com/A/B to a controller named content. Parameters A and B will be passed to method index.

Resources