Welcome Guest, Not a member yet? Register   Sign In
Rerouting to Different Controllers for Mobile/Wii at a Root Level
#1

[eluser]Jonathan Ledbetter[/eluser]
I'm trying to figure out the best way to reroute input for cell phones and Wii. (I've already whipped up a couple of custom methods, isMobile() and isConsole(), in a helper function for this purpose.) Using these three functions, I can think of three ways to do it:

1. Capture the output before being sent to a controller. I tried to implement this in both my index.php file and my application/configs/routes.php file (by wrapping an isMobile() check around a $routes declaration), but to no avail.

2. Use a _remap() declaration in each controller.

3. Load different views for mobile/Wii users.

Now, I can see where it would be more useful to use some techniques over the other. I'd rather lean towards option #1 and option #3. While #3 would reduce the potential code by a third, the first one would allow me to keep things better organized.

My question is, is there a way to use my custom mobile/Wii functions to reroute to different controllers at a basic level? The functions I use are listed below:

Code:
function matchBrowser($needle)
{
    $haystack = $_SERVER['HTTP_USER_AGENT'];
    return (stripos($haystack, $needle) !== FALSE);
}

function isMobile()
{
    $mobiles = array('opera mini',
                     'opera mobi',
                     'mobileie',
                     'iphone',
                     'ipod');
    
    $isMobile = false;
    
    foreach ($mobiles as $mobile):
        if (matchBrowser($mobile)):
            $isMobile = true;
            break;
        endif;
    endforeach;
    
    return $isMobile;
}

function isConsole()
{
    $consoles = array('wii');
    
    $isConsole = false;
    
    foreach ($consoles as $console):
        if (matchBrowser($console)):
            $isConsole = true;
            break;
        endif;
    endforeach;
    
    return $isConsole;
}

Any help in this matter would be greatly appreciated. Also, any idea of which way would be better/easier to maintain in the long run would also be very helpful. Thanks in advance!

(If it helps, I'll be testing on a Samsung SCH-i760 phone with Mobile IE and Opera Mobile, as well as my Wii.)
#2

[eluser]xwero[/eluser]
Why don't you use the user agent class?

If you have different views why not let your controller method do the work instead of rerouting?

Code:
// fetch data needed in all views
if ($this->agent->is_browser())
{
    // fetch extra data for browser
    $view = 'browser_page';
}
elseif ($this->agent->is_mobile())
{
    $view = 'mobile_page';
}

$this->load->view($view,$data);
#3

[eluser]Jonathan Ledbetter[/eluser]
xwero,

I'll admit that I have reservations about using CI's browser class, mostly from other people's experiences/frustrations with it. The big drawbacks I see with it is that it doesn't appear to have been updated for a long time (it still lists "Windows Longhorn"!), it lacks mobile recognition support for both browsers on my phone, and it doesn't mention Opera for Wii (or any other browser for standalone consoles) anywhere. The only gaming console I saw was PSP (and possibly Nintendo DS, I don't have it in front of me). Also, I didn't just want to add Opera for Wii in one of the lists; it's not quite a normal browser, and it's not quite a mobile browser, it really just needs its own category.

That, and because I'm trying to target only a specific number of current devices, have led me to decide to forgo the browser class and roll my own quick-and-dirty helper function. Until CI catches up to the browser list game, I'm sticking with my own functions.

Your suggestion was actually what I was trying to say I'd do in option #3. That's the way I'm leaning, more for a code-saving standpoint, so that I only need to call models from a minimal number of controllers and controller methods.

However, from a curiosity (and possible adaptation) standpoint, my question remains: Is there a way for CI to reroute to different controllers automatically based on what my functions return? You could even substitute the browser class calls, as long as they can be interchanged in the example.

I look forward to your responses, and thanks again.
#4

[eluser]xwero[/eluser]
You could (ab)use the routes.php.
Code:
// user agent function that identifies the browser
$route['(.+)'] = ua_browser().'/$1';
This assumes the controllers are all in a directories based on the name the ua_browser function outputs.




Theme © iAndrew 2016 - Forum software by © MyBB