GET params with URI rounting/ Codeigniter - codeigniter

$route['ajax/get/mail'] = "mail/get_mail_by_params";
I am trying to request *ajax/get/mail?user_id=123&foo=bar&bar=foo*
And params it in controller:
$foo = $this->input->get('foo')
But $_GET in ajax/get/mail variable is empty!
I suggest, that routing doesn’t supports GET paramets. What to do?

Have you tried using the MY_Input library? http://codeigniter.com/wiki/MY_Input/
Also, I think you may need to update your URI Protocol in config.php to PATH_INFO.
$config['uri_protocol'] = "PATH_INFO";

Related

CodeIgniter query string url and segment base url?

I want to use a segment base URL and a query string URL with CodeIgniter at the same time, and in the same project, for example http://localhost/finance_new/login/logout and http://[::1]/finance_new/?c=login&m=logout.
When I set enable_query_string to true, it only works for the second pattern but not for the first.
How can I use both URLs?
I am using CodeIgniter version 3.0.
You probably forget to set base_url() in application/config/config.php.
Just set base_url() like this..
$config['base_url'] = 'http://localhost/finance_new/';
Then
$config['enable_query_strings'] = TRUE;

Load controller without $route

exmple: this load default controller/class with function page,
www.example.com/page
unless we have controller/class named page AND set $route['page'] = 'page'; it'll load the controller. But if we dont set the $route, it'll still load default_controller.
is that true a controller must have a $route[''] always? is not it possible to load controller page without set $route[''] even there is no default controller function with same name?
Edit:
I access
www.mysite.com/index.php/user
I do have user controller with index function, but my route file only contain:
$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';
$route['product'] = 'product';
//$route['user'] = 'user';
$route['404_override'] = '';
returns 404, only works if I uncomment this: $route['user'] = 'user';
why?
Thanks.
No, that's not true. CodeIgniter, by default, directly maps URI segments to:
example.com/index.php/controller/method/param/param/...
Or if you have an .htaccess / similar solution to remove index.php:
example.com/controller/method/param/param/...
Routing is used when you wish to use a URL that does not directly map to this convention.
Edit: You have conflicting routes. CodeIgniter will look at each route in order from top to bottom, and if it find one that matches, it stops looking and processes that route. Because you have an (:any) catch-all route, it will match anything (like it says).
The rule of thumb is to place your most specific routes first, and then get more generic and catch-all later. Your (:any) route should be the very last one in your list. And the default controller and 404 overrides should stay first.
$route['default_controller'] = 'page';
$route['404_override'] = '';
$route['product'] = 'product';
$route['user'] = 'user';
$route['(:any)'] = 'page/$1';
You need to add the product and user routes because you've defined the (:any) route. If you want to avoid writing route rules for every one of your existing controllers, but still take advantage of a catch-all controller, consider using the 404_override controller/method instead. You can do your verifications to check if the URI is valid there. Just make sure to throw a 404 error if not (you can use show_404()), since any non-existant URL will be routed to there.

CodeIgniter - When using $route['(:any)'] = 'pages/view/$1' how to use other controllers?

When using
$route['(:any)'] = 'pages/view/$1';
and I want to use other controllers in my routing for example:
$route['del/(:any)'] = 'crud/del';
it won't work. I guess it will use
pages/view/del/$1
and not my crud-controller when deleting an item. How can I solve this?
As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:
$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
Its hundred percent working
$route['(:any)'] url is placed last in your routes file
$route['(:any)/company_product_deal_detail'] = "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)'] = "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals'] = "mypage_service_deal_list/index/$1";
$route['(:any)'] = "company/index/$1";
I know that it's an old question, but I have found myself a nice solution.
By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:
In system/core/Router.php find _parse_routes method.
Add this code under literal route match:
$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
return $this->_set_request($cont_segments);
}
I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.

CodeIgniter: urlencoded URL in URI segment does not work

I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:
www.domain.com/foo/urlencoded-url/
Any ideas what's wrong? Should I do this via GET instead?
UPDATE:
// URL that generates 404
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
// This is in my profile_manager controller
public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')
If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/
It seems that the %2F breaks things for apache.
Possible solutions:
preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
Set apache to AllowEncodedSlashes On
bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
double url encode it before sending
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF
You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:
http://codeigniter.com/forums/viewthread/81365/
http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
Passing URL in Codeigniter URL segment
I actually had to do urlencode(urlencode(urlencode(
and urldecode(urldecode(urldecode(
3 times!! and it finally worked, twice didn't cut it.
try
function __autoload($class){
if(!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0] = urldecode($_SERVER['REQUEST_URI']);
}
}
in config.php
this method work for me
This is very old, but I thought I'd share my solution.
Instead of accepting the parameter as a url path, accept it as a get variable:
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
and in code:
function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
$url_current = $this->input->get('url_current');
...
This seems to work.

enable the query string in codeigniter

i already make two changes in config file to enable the $_GET array as
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = true;
but whenever i try tow run my code as http://example.com/controller/function/$demo=demo
it redirected towards the default controller
Enabling query strings in CodeIgniter means that you're using the query string to pass in the controller and function instead of them being parsed from the PATH INFO.
If you want to use the default system for determining what controller and function to use, you do not want to set $config['enable_query_strings'] to true.
This previous SO post covers how to enable teh $_GET array in CodeIgniter: Enabling $_GET in codeigniter
I think that's what you're trying to do.
You also have to set controller and function triggers from config:
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
So once query string is enabled, you can access it like this:
http://example.com/index.php?c=controller&m=function&demo=demo

Resources