Answers.Body: <p>If you go down the route of creating your own modules for craft/yii2:</p>
  
  <p>You can register access from twig to PHP services in your modules <code>init()</code> function</p>
  
  <pre><code>Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $e) {
  
          $e-&gt;sender-&gt;set('phpaccess',services\yourService::class);
  
  
      });
  </code></pre>
  
  <p>Access functions within this service in twig with</p>
  
  <pre><code>{% craft.phpaccess.yourfunctionname() %}
  </code></pre>
  
  <p>You could also return a twig template from your modules controller with some additional variables</p>
  
  <pre><code>$view = $this-&gt;getView();
  $html = $view-&gt;renderTemplate(
      '/_templatename.twig',
      [
          'additionalvar' =&gt; $somephpvar
      ]
  );
  
  return $html;
  </code></pre>
  
  <p>Also if you havn't already seen the <a href="https://pluginfactory.io/" rel="nofollow noreferrer">pluginfactory</a> utility, it does save a lot of time!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-30T13:37:20.047Z
Answers.LastActivityDate: 2019-01-30T13:37:20.047Z
Answers.UserDisplayName: Sam
Body: <p>I'm experienced in Yii2 framework, but I just discovered Craft CMS and I already love it.  </p>
  
  <p>I use to develop Yii 2 modules using their own DB tables, models, controllers and views.<br>
  I usually use Yii extensions and widgets from Krajee (<a href="http://demos.krajee.com/widgets" rel="nofollow noreferrer">http://demos.krajee.com/widgets</a>) for the frontend, to create rather complex forms.  </p>
  
  <p>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.  </p>
  
  <p>Which structure would you recommend ?  </p>
  
  <p>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) ?  </p>
  
  <p>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 ?</p>
  
  <p>Thanks in advance for your advice.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-22T09:52:33.867Z
Comments.Text: 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.
Comments.UserDisplayName: Jørgen
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-23T15:01:01.597Z
Comments.Text: 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" (https://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 ?(https://www.yiiframework.com/extension/yiisoft/yii2-twig/doc/guide/2.0/en/layouts-and-widgets)
Comments.UserDisplayName: Alain Rollmann
Tags: <modules><yii>
Title: Integrating Yii 2 modules and frontend widgets with Craft CMS 3
ThreadText: # Integrating Yii 2 modules and frontend widgets with Craft CMS 3
  
  (Asked by: username_0 on 2019-01-22)
  
  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.
  
  **Tags:** modules, yii
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: 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" (https://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 ?(https://www.yiiframework.com/extension/yiisoft/yii2-twig/doc/guide/2.0/en/layouts-and-widgets)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-01-30. Score: 1 | Accepted answer
  
  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) {
  
          $e->sender->set('phpaccess',services\yourService::class);
  
      });
  
  ```
  
  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(
      '/_templatename.twig',
      [
          'additionalvar' => $somephpvar
      ]
  );
  
  return $html;
  
  ```
  
  Also if you havn't already seen the [pluginfactory](https://pluginfactory.io/) utility, it does save a lot of time!
  
  
  ---
  
  