I'm experienced in Yii2 framework, but I just discovered Craft CMS and I already love it.

I use to develop Yii 2 modules using their own DB tables, models, controllers and views.
I usually use Yii extensions and widgets from Krajee (http://demos.krajee.com/widgets) for the frontend, to create rather complex forms.

I'd like to integrate such modules and widgets in some pages of a Craft-managed website, using Craft for user management/access control and templating if possible.

Which structure would you recommend ?

Should I keep a separate instance of Yii 2 on the same server ? If so, how can I integrate Yii2 views (PHP code) in Twig templates (no PHP) ?

Or should I redevelop Craft modules or plugins using Twig ? If so, how can I integrate Yii 2 frontend widgets and extensions in Twig templates and Craft CMS modules ?

Thanks in advance for your advice.

  • There should not be a need for a separate yii installation - craft requires it, so it's in the vendor folder. Twig files can have PHP in them, so you should just be able to use the extensions as is. Try that and update your question if it does not work.
    – Jørgen
    Commented Jan 22, 2019 at 9:52
  • 1
    OK, I think the best solution would be to develop a module to get access to Yii 2 and take advantage of the features of Craft backend. The difficulty is at the frontend: Yii views include PHP code. However, in the official Craft CMS documentation, we read: "PHP code is not allowed in your templates" (docs.craftcms.com/v3/dev). Perhaps the solution would be to install the yii2-twig extension, which helps using widgets in convenient way to convert their syntax to function calls ?(yiiframework.com/extension/yiisoft/yii2-twig/doc/guide/2.0/en/…) Commented Jan 23, 2019 at 15:01

1 Answer 1


If you go down the route of creating your own modules for craft/yii2:

You can register access from twig to PHP services in your modules init() function

Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $e) {



Access functions within this service in twig with

{% craft.phpaccess.yourfunctionname() %}

You could also return a twig template from your modules controller with some additional variables

$view = $this->getView();
$html = $view->renderTemplate(
        'additionalvar' => $somephpvar

return $html;

Also if you havn't already seen the pluginfactory utility, it does save a lot of time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.