CodeIgniter query string url and segment base url? - codeigniter

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;

Related

Make user-friendly URL in Codeigniter with multi-language support

I have created a multi-language website.
Facing issue while creating SEO user-friendly URL.
current URL:
http://localhost/example/en/user/myaccount OR
http://localhost/example/fr/user/myaccount
want to change it with
http://localhost/example/en/myaccount OR
http://localhost/example/fr/user/myaccount
routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl|id|es)/(.+)$'] = "$2";
// '/en', '/de', '/fr','/es' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl|id|es)$'] = $route['default_controller'];
also tried with
$route["myaccount"] = "user/myaccount";
$route["^(en|de|fr|nl|id|es)$/myaccount"] = "user/myaccount";
Nothing will work in this case. Already used following in routes.php file other demo projects with out multi-language. There it's work perfectly.
$route["myaccount"] = "user/myaccount";
Thanks in advance
you can consult the documentation for routes here
https://www.codeigniter.com/user_guide/general/routing.html
and for your problem
$route["(:any)/myaccount"] = "user/myaccount/$1";

`translate_uri_dashes` not working codeigniter route

i already tried this code in route file but it not convert _ to - by default
$route['translate_uri_dashes'] = TRUE;
$route['stock/upload_stock'] = 'stock/upload_stockt';
CodeIgniter 3 provides a nice way for it ,There is a route
$route['translate_uri_dashes'] = false;
which is by default set to false, but if you set it to true, you can name your controllers and controller methods using the underscores (_s) and can call them using dashes (-s).
For example you have a controller named Company, and inside your Controller you have a method called about_us, now you can call it both the ways /company/about_us and company/about-us , when your $route['translate_uri_dashes'] is set to true.
So, try making your route as below
$route['stock/upload-stock'] = 'stock/upload_stock';

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.

GET params with URI rounting/ 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";

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