serving different views in code igniter
I'm pretty new 开发者_如何学JAVAto code igniter. Is there best practice for serving different view for different context. For example, I'd like to serve specific pages for mobile user agents with the same controllers.
There isn't a hard rule for this. You can structure your view files however you like, and call $this->load->view()
to load different view files for different outcomes in your controller. From my experience, CodeIgniter adapts very openly to how you organize your application's files.
In your example, perhaps I'd divide my system/application/views
folder into two subfolders: main
for desktop browsers, and mobile
for mobile browsers:
system/
application/
views/
main/
index.php
some_page.php
...
mobile/
index.php
some_page.php
...
In an early part of your controller, say the constructor, you can decide what user agent is requesting it and then pick main
or mobile
based on that, then show your views accordingly from your controller actions.
Some quick code snippets to give you a better idea since you're new...
// Place this just below the controller class definition
var $view_type = 'main';
// Controller constructor
function MyController()
{
parent::Controller();
if ($this->agent->is_mobile())
{
$this->view_type = 'mobile';
}
else
{
$this->view_type = 'main';
}
}
// Example action
function some_page()
{
// ...
// This comes from the 'var $view_type;' line above
$this->load->view($this->view_type . '/some_page');
}
And some helpful references for you to explore:
- Views in CodeIgniter
- User Agent Class
Hope my explanation helps, and hope you have fun with CodeIgniter :)
精彩评论