Codeigniter routes not working, possibly because of internationalization? - php

Working on codeigniter project.
Got stuck with routes.
I want to access en/company/login through en/login, so how do I define routes then?
Right now routes code looks like this:
// URI like '/en/about' -> use controller 'about'
$route['^(en|lv)/(.+)$'] = "$2";
// '/en', '/lv' URIs -> use default controller
$route['^(en|lv)$'] = $route['default_controller'];
$route['company/login'] = "login";
tried:
$route['^(en|lv)/company/login'] = "login";
Obviously, Im not getting something.
Can you help please?

Your problem is (i had the same) that this:
$route['^(en|lv)/(.+)$'] = "$2";
overrides your rule for:
$route['^(en|lv)/company/login'] = "login";
Try this:
$route['^(en|lv)\/(company)\/(.+)$'] = "login";
Maybe it should help you to override the first pattern
And if not, edit the first pattern to this:
$route['^(en|lv)\/(?!company).+$'] = "$2";

$route["(en|fr|gr)/(:any)/login"] = "login/index/$1";
--
public function index($company)
{
$language = strtolower($this->uri->rsegments[3]);
if(!in_array($language, ['en', 'fr'])){
// set a default language
// if the route does not provide a valid one
$language = 'en';
}
// Load the language file for the selected language
// for example english, language/en/en_lang.php
$this->lang->load($language, $language);
}
Thank you for your answer, but it seems that I didnt explain that
/company/ is controller and /login/ is "company" function. Your
solution wont work for me. :/
// The $route should contain the uri,
// you seem to have them mixed up.
// $route['uri'] = "controller/method"
$route['(en|lv)/login'] = 'company/login';

Related

url codeigniter multilanguage for all pages

I have base url localhost/new/ for multi language website. When changing language language appears in the url localhost/new/en etc. The problem is when I change a page the language disapears localhost/new/popular_tests, when staying on that page I change the language the url goes back to localhost/new/en. The aim is to add language to the certain page like localhost/new/popular_tests/en etc.
I have in controller:
public function changeLang($lang_code=''){
$this->lang->load('main', $lang_code=='' ? 'english' : $lang_code);
$this->session->set_userdata('language',$lang_code);
redirect(base_url().''.$lang_code);
}
in the config/route.php I have:
$route['default_controller'] = 'home';
$route['(:any)/test'] = 'home/test/$1';
$route['popular_tests'] = 'home/popular_tests';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;
$route['^(\w{2})/(.*)$'] = '$2';
$route['^(\w{2})$'] = $route['default_controller'];
Great thanks in advance.
That's because you have a routedefined as $route['^en$'] = $route['default_controller']; which means any url ending with en will point to default_controller.
Try Replacing that route with this:
$route['^new/(:any)/en$'] = 'home/$1';
$route['(:any)/new/en$'] = $route['default_controller'];
PS: This is untested version
You should use this one,
$route['(\w{2})/test'] = 'home/test';
$route['(\w{2})/(.+)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];

codeigniter routing uri with default_controller

Hi everyone im new with codeigniter. my routes.php is
$route['default_controller'] = "Maincontroller";
$route['(:any)'] = "Maincontroller/user_index/$1";
i want to search people if they type in the URL = www.site.com/username
but my problem is when going to other controller. should i route all my controllers?
$route['default_controller'] = "Maincontroller";
$route['somecontrollers'] = 'somecontrollers';
$route['(:any)'] = "Maincontroller/user_index/$1";
then how about my methods.
i tried this remap
public function _remap($method, $params = array())
{
if (method_exists(__CLASS__, $method)) {
$this->$method($params);
} else {
$this->user_index($method);
}
}
but this only works properly in controller which is not default, and i get the result i want. but as i apply this in my default controller it doesnt work well.
Hope this one help you!
$route['Maincontroller/(:any)'] = 'Maincontroller/user_index/$1';
$route['somecontrollers/(:any)'] = 'somecontrollers/user_index/$1';
$route['(:any)'] = "user_index/$1";

Rewrite URL if controller is not existing in codeigniter

Rewrite URL if user tried to access any non existing controller.
Ex:- If user tried to access http://example.com/project/anyvalue . In my program there is no controller with name 'anyvalue'. In this situation I want to redirect to
http://example.com/project/profile/anyvalue
How is this possible using routing in codeigniter?
Use default route to redirect requests to some particular page if controller is missing
You can find routes location in
/application/admin/config/routes.php
$route['default_controller'] = "welcome";
Also use following in case of page not found
$route['404_override'] = 'default_page';
Add routes to all existing controllers under "/project/..."
Add a route that will match any paths under "/project"
Example:
/* Currently available controllers under "/project/" */
$route['project/profile'] = "project/profile";
$route['project/add'] = "project/add";
$route['project/edit'] = "project/edit";
/* Catch all others under "/project/" */
$route['project/(:any)'] = "project/profile/$1";
/* if controller class name is Profile and function name is index */
$route['project/(:any)'] = 'project/profile/index/$1';
What you want is Vanity URLs, you can find a guide for performing this in code igniter here:
http://philpalmieri.com/2010/04/personalized-user-vanity-urls-in-codeigniter/
Essentially you're adding this to your routes file:
$handle = opendir(APPPATH."/modules");
while (false !== ($file = readdir($handle))) {
if(is_dir(APPPATH."/modules/".$file)){
$route[$file] = $file;
$route[$file."/(.*)"] = $file."/$1";
}
}
/*Your custom routes here*/
/*Wrap up, anything that isnt accounted for pushes to the alias check*/
$route['([a-z\-_\/]+)'] = "aliases/check/$1";

Codeigniter URL remapping strategy

I'm working on a project built in codeigniter that makes heavy use of routes and the remap function to rewrite urls. The current implementation is confusing and messy.
Essentially this is what the designer was trying to accomplish:
www.example.com/controller/method/arg1/
TO
www.example.com/arg1/controller/method/
Can anyone suggest a clean way of accomplishing this?
This actually only needs to happen for one specific controller. It's fine if all other controllers need to simply follow the normal /controller/model/arg1... pattern
Just to give you an idea of how the current code looks here is the 'routes' file: (not really looking into any insight into this code, just want to give you an idea of how cluttered this current setup is that I'm dealing with. I want to just throw this away and replace it with something better)
// we need to specify admin controller and functions so they are not treated as a contest
$route['admin/users'] = 'admin/users';
$route['admin/users/(:any)'] = 'admin/users/$1';
$route['admin'] = 'admin/index/';
$route['admin/(:any)'] = 'admin/$1';
// same goes for sessions and any other controllers
$route['session'] = 'session/index/';
$route['session/(:any)'] = 'session/$1';
// forward http://localhost/ball/contests to controller contests method index
$route['(:any)/contests'] = 'contests/index/$1';
// forward http://localhost/ball/contests/vote (example) to controller contests method $2 (variable)
$route['(:any)/contests/(:any)'] = 'contests/index/$1/$2';
// forward http://localhost/ball/contests/users/login (example) to controller users method $2 (variable)
$route['(:any)/users/(:any)'] = 'users/index/$1/$2';
// if in doubt forward to contests to see if its a contest
// this controller will 404 any invalid requests
$route['(:any)'] = 'contests/index/$1';
$route['testing/'] = 'testing/';
And the remap function that goes with it:
public function _remap($method, $params = array()){
// example $params = array('ball', 'vote')
// params[0] = 'ball', params[1] = 'vote'
/*
* Write a detailed explanation for why this method is used and that it's attempting to accomplish.
* Currently there is no documentation detailing what you're trying to accomplish with the url here.
* Explain how this moves the contest name url segment infront of the controller url segment. Also
* explain how this works with the routing class.
* */
$count = count($params);
if($count == 0){ // no contest specified
redirect('http://messageamp.com');
return;
}
$contest_name = $params[0];
unset($params[0]); //remove the contest name from params array because we are feeding this to codeigniter
if($count < 2) // no method specified
$method = 'index';
else{
$method = $params[1];
unset($params[1]);
}
//We need to scrap this, lazy-loading is a best-practice we should be following
$this->init(); //load models
//make sure contest is valid or 404 it
if(!$this->central->_check_contest($contest_name)){
show_404();
return;
}
$this->data['controller'] = 'contests';
$this->data['method'] = $method;
$this->data['params'] = $params;
// call the function if exists
if(method_exists($this, $method)){
return call_user_func_array(array($this, $method), $params);
}
show_404(); // this will only be reached if method doesn't exist
}
To get something like this:
www.example.com/controller/method/arg1/ TO www.example.com/arg1/controller/method/
You could do this in your routes.php config:
$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";
However, if you want to have all of your other classes stick to the default routing, you would need to create routes for each of them to overwrite this default route:
$route['controller_name/(:any)'] = "controller_name/$1";

How to hide controller name in the url in CodeIgniter?

so the thing is I'm using .htaccess to hide the index.php but I still get the controller name in the url like that: http://example.com/name_controller/about
My question is: is it possible to hide the name of the controller, so that only method is shown? hxxp://example.com/name_controller/about
You can define a custom route in config/routes.php - for example:
$route['about'] = 'name_controller/about';
Then, http://example.com/about
goes to http://example.com/name_controller/about
See Hiding the controller’s method name in the URL? in the CI forums for more information.
You can add an entry in the /system/application/config/routes.php file:
$route['about'] = "controller_name/about";
$route['default_controller'] = "xxx";
home
$route['home'] = "xxx/home";
function_name/parameter0/parameter1/parameter2
$route['Collection/(:any)'] = "xxx/Collection/$1";
I did it like this: (config/routes.php)
Code:
$route['((photos|blogs).+)'] = "$1";
$route['([a-zA-Z0-9_-]+)'] = "user/profile/$1";
it is ok correct solutions for common.
You can add below code in the /application/config/routes.php file:
$route['default_controller'] = "Home";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(?i)about'] = "Home/about";
$route['(?i)login'] = "Home/Login";
$route['(?i)products'] = "ProductController/ProductList";
$route['(?i)addproduct'] = "ProductController/AddProduct";
$route['(?i)editproduct'] = "ProductController/EditProduct";
$route['(?i)products/(:any)'] = "ProductController/ProductList/$1";//search product list perameters.
100% It work..

Categories