简体   繁体   English

Codeigniter为移动设备加载不同的控制器

[英]Codeigniter loading different controllers for mobile

I am looking for solution how to load different controllers not changing website uri for mobile devices. 我正在寻找解决方案,如何在不更改移动设备网站uri的情况下加载不同的控制器。 For example I'm going to www.site.com via PC and I should see webpage (works by default, everything is ok). 例如,我要通过PC转到www.site.com,我应该看到网页(默认情况下可以正常运行,一切正常)。 But now I'm going www.site.com via mobile and I want to see mobile version of this website not changing url's. 但是现在我要通过手机访问www.site.com,我希望看到该网站的移动版本不会更改url。 So when I will go to www.site.com/article-123 via mobile, I want to see mobile version of page. 因此,当我通过移动设备访问www.site.com/article-123时,我想查看页面的移动版本。 I'm looking for solution, but I haven't found anything. 我正在寻找解决方案,但没有发现任何东西。 I tried to load user_agent library to my routes file, but nothing happened (wanted to change call to controllers for mobile devices). 我试图将user_agent库加载到我的路由文件中,但是没有任何反应(希望更改对移动设备控制器的调用)。 Is there any solution? 有什么解决办法吗?

Quote OP : 引用OP

"I tried to load user_agent library to my routes file, but nothing happened." “我试图将user_agent库加载到我的路由文件中,但是什么也没发生。”

I don't know how you'd add a library to the routes since you never showed us, but see the documentation for proper usage of the user_agent library. 由于您从未展示过我们,因此您不知道如何向routes添加库,但请参阅文档以了解user_agent库的正确用法。 Then use the url helper to redirect to a different Controller function. 然后使用url帮助redirect到另一个Controller函数。

$this->load->library('user_agent');
$this->load->helper('url');

if ($this->agent->is_mobile())
{
    redirect('/mobile/home/', 'refresh'); // go to 'mobile' controller, 'home()' function
}
else
{
    redirect('/desktop/index/', 'refresh'); // go to 'desktop' controller, 'index()' function
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM