Welcome Guest, Not a member yet? Register   Sign In
Dynamic pages, best practices for routing/parsing

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:
and for russian

While articles could be:

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?

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

// 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

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

Wont be better using this:


[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

[eluser]Mr. Ed[/eluser]

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


thanks Colin.

Your code very useful for me!

// 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.

Theme © iAndrew 2016 - Forum software by © MyBB