CodeIgniter Forums
Rename controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Rename controller (/showthread.php?tid=1051)



Rename controller - superheld - 02-08-2015

Hi!

I'm new in oop and ci and there is a point, where i have no idea for a nice solution:

My first project should be available in different languages. But how can i bring the nice URIs to an other language? I think routes are my friends, but is this the way of choice?

I have in german

Code:
/karte/erstellen

This should in english

Code:
/card/create

As i told, i'm new. So what is the finest way to rename my controllers and methods for a new language?

Thanks Smile


RE: Rename controller - Avenirer - 02-09-2015

I was just trying to do a tutorial about multilanguage site subject (http://avenir.ro/create-cms-using-codeigniter-3/create-multilanguage-site-codeigniter/), and I got to this part that you are asking about. What I was thinking is something in the lines of this:

I thought of doing the language things inside the routes.php. First of all, I associated "controllers" and "methods" of other languages to the controllers and methods we have defined (this one is done manually, unless you think of another method):

PHP Code:
$controllers_methods = array(
 
   'de' => array(
 
       'willkommen/list' => 'welcome/list',
 
       'willkommen' => 'welcome'
 
   ),
 
   'fr' => array(
 
       'bienvenu/list' => 'welcome/list',
 
       'bienvenu' => 'welcome'
 
   )
); 
The thing is that, when you write the uris you must write the biggest uris first (the ones that have methods attached to controllers), and leave the shortest ones (the ones that have only controllers) at the end.

Then, I just return a callback function when someone asks for a controller and method in a particular language:
PHP Code:
$route['^(\w{2})/(.*)'] = function($language$link) use ($controllers_methods)
{
 
   foreach($controllers_methods[$language] as $key => $sym_link)
 
   {
 
       if(strrpos($link$key,0) !== FALSE)
 
       {
 
           $new_link ltrim($link,$key);
 
           $new_link $sym_link.$new_link;
 
           break;
 
       }

 
   }
 
   return $new_link;
};
$route['^(\w{2})$'] = $route['default_controller']; 

So, in the end the router.php would look something like this:
PHP Code:
<?php

defined
('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

$controllers_methods = array(
 
   'de' => array(
 
       'willkommen/list' => 'welcome/list',
 
       'willkommen' => 'welcome'
 
   ),
 
   'fr' => array(
 
       'bienvenu/list' => 'welcome/list',
 
       'bienvenu' => 'welcome'
 
   )
);

$route['^(\w{2})/(.*)'] = function($language$link) use ($controllers_methods)
{
 
   foreach($controllers_methods[$language] as $key => $sym_link)
 
   {
 
       if(strrpos($link$key,0) !== FALSE)
 
       {
 
           $new_link ltrim($link,$key);
 
           $new_link $sym_link.$new_link;
 
           break;
 
       }

 
   }
 
   return $new_link;
};

/* End of file routes.php */
/* Location: ./application/config/routes.php */ 

What do you think? Any inputs?


RE: Rename controller - superheld - 02-18-2015

HI! I will just say thank you at the moment. I have some other issues with my project, so i work on this part a little bit later. I will comḿent what i did then and how it works.