Welcome Guest, Not a member yet? Register   Sign In
routes, urls and languages
#1

[eluser]webrix[/eluser]
Hi,

I'm trying to write a multi language application, here my situation:

- I have a database with the pages translated
Code:
CREATE TABLE IF NOT EXISTS `pages` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` tinytext NOT NULL,
  `keywords` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `language` varchar(255) NOT NULL,
  `parent_id` int(11) NOT NULL,
  `active` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

- I have the URI Language library

what I want to do is have url that look like this

<host>/it/azienda/chi-siamo/4
<host>/en/company/who-we-are/3

I write a route like this
Code:
$route['it/azienda/(:any)/(:any)'] = "company/show/($2)";

but it return 404

what is the best way to handle my situation?

thanks
#2

[eluser]devastator[/eluser]
I use this http://maestric.com/doc/php/codeigniter_i18n
#3

[eluser]mddd[/eluser]
You should really check in the server log to see what page it is trying to access but I'm guessing the problem is that you are putting brackets in the url.
I think the route should be
Code:
$route['it/azienda/(:any)/(:any)'] = 'company/show/$2';  // so not ($2) at the end
#4

[eluser]webrix[/eluser]
I have tried to put this route
Code:
$route['it/azienda/(:any)/(:any)'] = 'company/show/$2';
but still doesn't work, here are the logs:
Code:
DEBUG - 2010-05-31 09:25:03 --&gt; Config Class Initialized
DEBUG - 2010-05-31 09:25:03 --&gt; Hooks Class Initialized
DEBUG - 2010-05-31 09:25:03 --&gt; URI Class Initialized
ERROR - 2010-05-31 09:25:03 --&gt; 404 Page Not Found --&gt; azienda

what could it be?
#5

[eluser]mddd[/eluser]
Okay, from the CodeIgniter log it seems that the system is looking for a controller called 'azienda'. So it seems the route is not working.
What url are you requesting? Does it match your route? For instance if you ask for /azienda/../.. that will not work because it has to start with /it..
#6

[eluser]webrix[/eluser]
I have installed i18n

but the results seems the same

routes
Code:
$route['default_controller'] = "home";
$route['scaffolding_trigger'] = "";

// URI like '/en/about' -> use controller 'about'
$route['^it/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";

// '/en' and '/fr' URIs -> use default controller
$route['^it$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];

$route['it/azienda/(:any)/(:any)'] = 'company/show/$2';

url requested
Code:
<hosto>/it/azienda/chi-siamo/2

logs
Code:
DEBUG - 2010-05-31 09:55:49 --&gt; Config Class Initialized
DEBUG - 2010-05-31 09:55:49 --&gt; Hooks Class Initialized
DEBUG - 2010-05-31 09:55:49 --&gt; URI Class Initialized
ERROR - 2010-05-31 09:55:49 --&gt; 404 Page Not Found --&gt; azienda
#7

[eluser]mddd[/eluser]
I think the problem is in this route:
Code:
$route['^it/(.+)$'] = "$1";
This route not only matches a route like /it/about, but also matches your it/azienda/chi-siamo/2 !!

After the first match, the routing stops. So basically the system will just remove /it from the front and then try to run the controller.
You should change the route so it only matches a call with 1 segment. Like so:
Code:
$route['^it/(:any)$'] = '$1';

(At least, I think ":any" will match anything but not a "/". In other words, ":any" will only match 1 segment, not multiple segments).

Edit: I was wrong, ":any" is the same as ".*".
So you will have to check yourself that you only have 1 segment:
Code:
$route['^it/([^/]+)$'] = '$1';
#8

[eluser]webrix[/eluser]
ok
Code:
$route['it/azienda'] = 'company';
$route['it/azienda/(:any)'] = 'company';
$route['it/azienda/(:any)/(:any)'] = 'company/show/$2';

this seems to solve the problem

now the urls should look like
Code:
<host>/it/azienda/chi-siamo/2
<host>/en/company/who-we-are/3

but it's not so simple to handle many urls and to switch language in the same page.

In the database I have an url field that is "chi-siamo" or "who-we-are", what's the best way to handle URL dynamically?
#9

[eluser]mddd[/eluser]
Do you really need to put the language in the url for every page? The numbers 2 or 3 in the url are probably the id's of your pages, correct?

That means that you don't need to put the language in the url. From looking at /azienda/chi-siamo/2 the system can know that the language is Italian. You can see this because in your route, you are throwing away the 'it' part! So you can just forget about the /en or /it part here. Just use it where it is necessary! And not anywhere else.
#10

[eluser]webrix[/eluser]
but the /en and /it is good per SEO so, all the italian website is under this dir... right?




Theme © iAndrew 2016 - Forum software by © MyBB