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

[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?
#2

[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
}
#3

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

Wont be better using this:

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

[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
#5

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB