Welcome Guest, Not a member yet? Register   Sign In
Trick to delete controller in query string
#1

[eluser]Cahyono[/eluser]
I try to change http://localhost/index.php?c=welcome&m=home into http://localhost/index.php?m=home

So I want to delete c=welcome from URL so that shorter. After trial and error, I found like this:

in file system\application\config\routes.php :

Code:
$route['default_controller'] = "welcome";


in file system\Libraries\Router.php search on function _validate_request($segments):

Code:
// Can't find the requested controller...
show_404($segments[0]);

Add above so become this below:

Code:
$segments[0] = $this->default_controller;
return $segments;
// Can't find the requested controller...
show_404($segments[0]);

That's it. It's work fine to me.
#2

[eluser]Jelmer[/eluser]
That's by far not the best way to do this, in fact it's a pretty bad one since you'd have to edit your /system/ files on every CI update. Also it's bad because there's a pretty simple way to do the same within the system\application\config\routes.php file.

If I understand you correctly, you want every request to be handled by the same default controller if no controller can be found. While there are I believe even better ways to do this, the way I solved this problem was by first registering all my other controllers and then closing with the default router which is set to 'welcome' by the final rule:
Code:
$route[':any'] = 'welcome';

All the others you'll have to set before that rule by adding them:
Code:
$route['other_controller/(:any)'] = 'other_controller/$1';

Or with 2 rules if you want it to work without the function set as well:
Code:
$route['other_controller/(:any)'] = 'other_controller/$1';
$route['other_controller'] = 'other_controller';

I'll admit it's a bit more work, but keeping the system files without edits will save you many headaches when updating CI to the latest version.
#3

[eluser]Cahyono[/eluser]
Yes you right. Thanks for your suggestion.
#4

[eluser]Thorpe Obazee[/eluser]
[quote author="Jelmer" date="1228206340"]That's by far not the best way to do this, in fact it's a pretty bad one since you'd have to edit your /system/ files on every CI update. Also it's bad because there's a pretty simple way to do the same within the system\application\config\routes.php file.

If I understand you correctly, you want every request to be handled by the same default controller if no controller can be found. While there are I believe even better ways to do this, the way I solved this problem was by first registering all my other controllers and then closing with the default router which is set to 'welcome' by the final rule:
Code:
$route[':any'] = 'welcome';

All the others you'll have to set before that rule by adding them:
Code:
$route['other_controller/(:any)'] = 'other_controller/$1';

Or with 2 rules if you want it to work without the function set as well:
Code:
$route['other_controller/(:any)'] = 'other_controller/$1';
$route['other_controller'] = 'other_controller';

I'll admit it's a bit more work, but keeping the system files without edits will save you many headaches when updating CI to the latest version.[/quote]

Could it be done to use one routing rule to get this?
Code:
$route['other_controller/(:any)'] = 'other_controller/$1';
$route['other_controller'] = 'other_controller';




Theme © iAndrew 2016 - Forum software by © MyBB