nested module : view file inheritance

Hi,

I have nested modules where child module controllers inehrit from parent module controller. It works fine, but now I want to achieve sometinhg like view inheritance : if a view file is not found in the child module, search or it in its parent module.

Any idea how to achieve this ?

ciao

B)

I found a solution that is not the best one, but is enough for my requirements : overload the yii\web\View

  1. We have nested modules : Module father -> Module child

  2. Create a ModView class an override the findViewFile() method




namespace app\modules\father\components;


use Yii;


class ModView extends \yii\web\View {

	

   // this is a *very* simplified version of the parent method !!

	protected function findViewFile($view, $context = null)

	{		

		$lastPart = DIRECTORY_SEPARATOR . Yii::$app->controller->id . DIRECTORY_SEPARATOR . $view . '.php';

		$path =  Yii::$app->controller->module->getViewPath() . $lastPart;

		if( ! file_exists($path)){

			$path =  Yii::$app->controller->module->module->getViewPath() . $lastPart;

		}

		return $path;

	}



3) in the child module, register the view component to use and set it to ModView.




namespace app\modules\father\mod\child;


use Yii;


class Module extends \yii\base\Module

{

	public $controllerNamespace = 'app\modules\father\mod\child\controllers';


	public function init()

	{

    	parent::init();

    	Yii::$app->set('view',[

    		'class' => 'app\modules\father\components\ModView'

    	]);

	}

}



Now if in the child module we call render(‘index’) and if the default file view is not found in the child module, the view file will be retrieved from the parent module.

Note that ModView.findViewFile() is a simplification of the base class method : it does not support alias or ‘//’ or other ways of specifying a view name.

ciao

B)