I’m still investigating on different frameworks for my new projects. As my new projects require a different frontend, I was wondering whether Yii offers or any 3rd parties product that offers such capability.
An sample scenario: Display a storefront for user.
I will be using a Store controller to access the method/route display(). This display will grab the data from different models and display it out in a view. Depending on a param, $_GET[‘v’]=iphone or $_GET[‘v’]=web, it will grab a different view and throw it out.
Is it possible to do it implicitly without specifying if else in the controller? I’m sorry if this is a noob question as I’m still reading through the documentation and review. I’m very incline towards Yii but I will lean towards a framework that enable easy customisation of such features
If I got you right you want different views/layouts based on a $_GET variable.
First of all you can change the layout inside of a controller by changing the $layout variable. The layout is the skeleton for the actual content.
Inside of a controller you will usually call the render() method in order to display the actual content-view (which gets automatically surrounded by the layout).
Now in order to automatically set views based on a get variable, you can create a parent-controller that will do the work for you by extending the render() method.
Simple example:
class ParentController extends CController
{
public function render($view,$data=null,$return=false)
{
if (isset($_GET['v']))
{
$this->layout .= "_{$_GET['v']}";
$view .= "_{$_GET['v']}";
}
return parent::render($view,$data=null,$return=false);
}
}
Now let’s assume you set your default layout to “index” and you render() the view called “items”. Also $_GET[‘v’] is set to “iphone”. Now what happens is the parent-controller will change “index” to “index_iphone” and “items” to “items_iphone”. So based on the $_GET variable you have different views.
There are probably better ways to do this, but it should show you at least how easy it is at the end.