In Yii 1.x, you need to change the view-files, if in $_SERVER['HTTP_USER_AGENT'] is a word: MobBrowser for example.

In the protected/components/Controller.php file, I added:

 class Controller extends CController { public function render($view,$data=null,$return=false) { if ($_SERVER['HTTP_USER_AGENT']=='MobBrowser') $view = 'm_' . $view; return parent::render($view,$data=null,$return=false); } } 

Everything works, but can it somehow be more properly done?

    1 answer 1

    1) Instead of $_SERVER['HTTP_USER_AGENT'] you can use Yii::app()->request->getUserAgent() , it already checks if the HTTP_USER_AGENT index HTTP_USER_AGENT in the request, and if it is not in this example, an unpleasant Notice will crash. .

    2) Return simply return parent::render($view, $data, $return) , without assigning other values ​​to variables, otherwise you $data and $return will always be null and false respectively, no matter what you pass to the method render() .

     class Controller extends CController { public function render($view, $data = null, $return = false) { if (Yii::app()->request->getUserAgent() == 'MobBrowser') { $view = 'm_' . $view; } return parent::render($view, $data, $return); } } 

    3) For the subsequent convenient extension of the way to check the mobile browser and embed it in other parts of the code, it is worth creating a separate method in some common model / helper.