How to change layout of my module in my project

Hello, everybody,

I created my e-shop module, but I have a problem. To make this module reusable I need to define the layout in the project and not in the module as it is currently the case for my connection system.

But I don’t know how to do this I tried several things, extending the module controller and defining the layout but without success. I could rewrite the action in question to adapt the layout however it wouldn’t make sense for a module to rewrite a controller.

To give an example i have my UserController in my module with the basic actions actionLogin actionRequest actionRegister with no layout assigned.

and in my project I have extended the UserController with an init() function:

public function init()
{
    $this->layout = '@app/views/layouts/blank';
    parent::init();
}

but it doesn’t work.

If you have documentation or a track I’m interested.

Thank you very much for reading me. And sorry for my poor English.

Call parent first?

it does not work

class UserController extends \shop\frontend\controllers\UserController
{
    public function init()
    {
        parent::init();
        $this->layout = '@app/views/layouts/blank';
    }
}

Extend render()?
https://www.yiiframework.com/doc/api/2.0/yii-base-controller#render()-detail

the render() is specified in module actions such as this one :

public function actionLogin()
{
    if (!Yii::$app->user->getIsGuest()) {
        return $this->goHome();
    }

    $loginForm = new \shop\models\Form\LoginForm();
    if ($loginForm->load(Yii::$app->request->post()) && $loginForm->login()) {
        return $this->goBack();
    }
    return $this->render('login', ['model' => $loginForm]);
} 

or maybe I didn’t understand your point?

Same way as your init() but try render() instead. (guessing, not tried)

I’ve tried different ways, but nothing works.

return $this->render('@app/views/layouts/blank');

I came to wonder if what I was doing in my controller that extends my UserController actions was being read.

it may be a little more complex. Each part of the module is managing a view that I have modified in the project like this:

Capture

Can we also apply a layout to these views? how to make my extended controller read before the main controller in the module.

I might get lost.

I don’t know about the view thing so far, but I tried to change the layout in a module.
(I don’t have my own module so I tried gii DefaultController)
Use the Yii debugger to check which layout file is loaded.
(last two lines in Logs are view and layout).

it’s the default layout. my controller extends is not read.

I found the solution! If my controller wasn’t read it’s because the project view is in the folder: /modules/shop/user.

I took out the user folder to go to the root of the views folder and it works perfectly, it takes into account my layout.

Thank you @tri for your help it allowed me to think differently I don’t know yet if it’s the right solution but for the moment it works perfectly !