CodeIgniter Forums
Dynamic pages, best practices for routing/parsing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Dynamic pages, best practices for routing/parsing (/thread-10569.html)



Dynamic pages, best practices for routing/parsing - El Forum - 08-05-2008

[eluser]Unknown[/eluser]
Hi there.

I'm a long-time PHP developer currently developing my first website in CI and so far been enjoying myself. The website is a multilanguage one that will sport dynamic pages (articles ordered in subcategories) mixed with a couple static methods.

I have a couple (6-8 per language) static methods in my controller that are not articles.

The url's for these are are for example:
website.se/se/products/
website.se/se/contact/
and for russian
website.se/ru/products/
website.se/ru/contact/

While articles could be:
website.se/se/article-number-one/

Other than that I want to redirect all other requests to the "se"-controller (or en/ru depending on language) article_loader method. Whats the best practice concerning this? I've looked into routes but it seems like alot of work to map up all the static methods as a route just to exclude them from a route like:

$route['se/(.*)'] = 'se/article_loader/$1';

Is there a better way to resolve this?


Dynamic pages, best practices for routing/parsing - El Forum - 08-05-2008

[eluser]Colin Williams[/eluser]
Don't forget that, in any aspect of your CI application, you have a unique, handy little tool: PHP

Code:
// All the langs we support
$langs = array('en','es','ru','se');

// Get at our first segment after index.php. Might need to adjust. Check your $_SERVER['REQUEST_URI']
list($i, $j, $check) = explode('/', $_SERVER['REQUEST_URI']);

// See if we found a match to our $langs
if (in_array($check, $langs))
{
   $_SESSION['lang'] = $check; // Set in a superglobal for later reference
   $route[$check] = ""; // Route to homepage
   $route[$check .'/(.*)'] = "$1"; // Route to everything after lang
}



Dynamic pages, best practices for routing/parsing - El Forum - 08-05-2008

[eluser]Mr. Ed[/eluser]
I'm new to CI, but...

Wont be better using this:

$this->uri->segment(1)?


Dynamic pages, best practices for routing/parsing - El Forum - 08-05-2008

[eluser]Colin Williams[/eluser]
No, Mr. Ed. $this references the current class. In controllers and models, $this includes the full CI object. We're not in a class in routes.php


Dynamic pages, best practices for routing/parsing - El Forum - 08-05-2008

[eluser]Mr. Ed[/eluser]
Sorry!

I didn't realise about that was in routes.php, read it wrong.


Dynamic pages, best practices for routing/parsing - El Forum - 08-13-2008

[eluser]Unknown[/eluser]
:coolmad:

thanks Colin.

Your code very useful for me!

Code:
// All the controller
$arr_controller = array('welcome','user','blog');

list($i, $j, $check) = explode('/', $_SERVER['REQUEST_URI']);

// See if we found a match to our $langs
if (!in_array($check, $arr_controller))
{
    $route['(.*)'] = 'user/id/$1';
}

please correct my code if it wrong. thanks.