Welcome Guest, Not a member yet? Register   Sign In
Loading different views when on a mobile browser

Hi all,

I'm kinda new to codeigniter, and totally new to the forums, so bear with me Smile

I'm building a site which need to show different views to a user when he is on a mobile browser. Now, I've googled and searched here on the forum, but I can't seem to find an answer.

What I could do, is add a check for the user agent in every function, and load the appropriate views. However, this will clutter the code in my controllers, because I would need to do this whole if else stuff every time.

There was another solution I've found, which dealt with making some changes in the index, and copying that to a subfolder. I saw this meant the mobile version of the pages would have another url though. But I need the desktop and mobile version of every version to have exactly the same URL.

So, what I would like very, very much, is to be able to let CI do this automatically for every view that's loaded, in the entire application:
If the user is on a mobile browser, always look for view files in the views/mobile/ folder, and if he's not on mobile, always look in the views/default/ folder.

Is this possible?

Thanks a lot in advance,


By the way: Some other similar questions to this one, where getting answers along the lines of: "Build it responsively with css, mediaqueries, javascript, etc...But as much as I would like to do that (and have done that before), The design (and the differences between the mobile and default versions) doesn't lend itself for responsive webdevelopment, at all Sad

[eluser]Alex Florea[/eluser]
Yes. It is posible. Use User Agent Class from CI.


I know that, that's what I mentioned as my first solution. But the question is: is there a way to put the 'check user agent'-code on one place in the application, and make it load views from another view-folder by default from that point on.

Thanks Smile

Have you tried MY_controller?

Once you determine its a mobile device, you can set a var that determines which folder to load it from.
I use a helper that load a specific folder/template depending on device.

//helper name: view($folder_to_load, $data_to_pass)
view('mobile, $data);

I'm not sure I understand correctly...

Do you mean I write my own helper, that checks the user_agent and loads the appropriate view with $this->load-view(), and then use that helper everytime I want to load a view, instead of $this->load->view() ?

If so, sounds like a plan Smile

Well almost.

Checks like the user agent should be done at the earliest, hence in the MY_Controller.
If its a mobile device one can set a flag and use that to load a smaller set of data for instance.

The helper can use this flag to determine which view.

Its not just the view, remote devices often require different css, images and datasets. All depending on screen size and screen resolution



Theme © iAndrew 2016 - Forum software by © MyBB