Same view both on frontend and backend

Hello,

I use the yii advanced template with frontend and backend. I have a complicated view with many calculations in my frontend for the logged in user and now I want to have the same in the backend for all users. Can I move the code in the common folder? How does this work? I don’t want to have the same code twice, as there are minor tweaks here and then.

Thanks!

Hi,

If the view is more or less "stand-alone" you could move it (for example) to:

common/views/whatever/someview.php

And call it in frontend and backend controllers like (for example):




return $this->render('@common/views/whatever/someview', [

   'model' => $model,

]);



If the view is tied to a specific action,

you could consider creating a "standalone-action" and map it in frontend and backend controllers.

Read (or just google "yii2 standalone action"):

http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html

Hope this helps.

Best Regards

yes, thanks. It is indeed more than the view, there are many lines of controller code. Now I try it with standalone actions, see how this works…

So with standalone actions it is only possible to get the whole action? I’d like to see the view in the commons folder more like a partial, because on the backend I have some more to show like breadcrumbs and so on. So I would render the view in the backend folder and inside the view I render the commons view as a partial, but the problem is the code in the controller…

Hey,

From what you wrote it sounds like a job for a widget:

http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html

Regards

Yeah, widget was the right hint, it works!

Thanks a lot ;D