Welcome Guest, Not a member yet? Register   Sign In
Rename controller
#2

(This post was last modified: 02-09-2015, 03:28 AM by Avenirer.)

I was just trying to do a tutorial about multilanguage site subject (http://avenir.ro/create-cms-using-codeig...deigniter/), 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?
Reply


Messages In This Thread
Rename controller - by superheld - 02-08-2015, 09:15 AM
RE: Rename controller - by Avenirer - 02-09-2015, 03:22 AM
RE: Rename controller - by superheld - 02-18-2015, 02:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB