Answers.Body: <p>Try reading <a href="https://docs.craftcms.com/v3/routing.html#advanced-routing" rel="nofollow noreferrer">this</a>.</p>
  
  <p><code>return $this-&gt;render('index', ['twitterFeed' =&gt; $twitterFeed]);</code> is correct in craft 3 you just need to do some custom routing in order to reroute the homepage to your controller/action.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T16:04:13.773Z
Answers.Text: Where do I put the controller file?
Answers.UserDisplayName: MAX POWER
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T18:46:33.067Z
Answers.Text: It would have to be as a [Craft plugin](https://docs.craftcms.com/v3/plugin-intro.html) or as a Yii2 Module - you can generate the boilerplate code for both here: https://pluginfactory.io/
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T14:50:55.107Z
Answers.LastActivityDate: 2018-06-21T14:50:55.107Z
Answers.UserDisplayName: dustfeather
Body: <p>Coming from a Yii2 background, I am accustomed to doing things in an MVC way.</p>
  
  <p>I have set up a demo Craft CMS application to have a play around with. I've created a "singles" page (homepage) and got it displaying data. What I want to know is, how do I pass custom variables to the view?</p>
  
  <p>For example I have a function that fetches tweets from my Twitter account, I would like this data to be displayed on the homepage. In Yii2 I would normally do the following:</p>
  
  <pre><code>&lt;?php
  
  namespace app\controllers;
  
  use yii\web\Controller;
  use app\services\TwitterFeedService;
  
  class SiteController extends Controller
  {
      public function index()
      {
          $twitterFeed = TwitterFeedService::getTweets();
  
          return $this-&gt;render('index', [
              'twitterFeed' =&gt; $twitterFeed,
          ]);
      }
  }
  </code></pre>
  
  <p>But since there is no controller or action for my homepage at the moment, I am not sure how I would go about doing this in Craft CMS.</p>
  
Tags: <php>
Title: Pass custom variable to view
ThreadText: # Pass custom variable to view
  
  (Asked by: username_0 on 2018-06-21)
  
  Coming from a Yii2 background, I am accustomed to doing things in an MVC way.
  
  I have set up a demo Craft CMS application to have a play around with. I've created a "singles" page (homepage) and got it displaying data. What I want to know is, how do I pass custom variables to the view?
  
  For example I have a function that fetches tweets from my Twitter account, I would like this data to be displayed on the homepage. In Yii2 I would normally do the following:
  
  ```
  <?php
  
  namespace app\controllers;
  
  use yii\web\Controller;
  use app\services\TwitterFeedService;
  
  class SiteController extends Controller
  {
      public function index()
      {
          $twitterFeed = TwitterFeedService::getTweets();
  
          return $this->render('index', [
              'twitterFeed' => $twitterFeed,
          ]);
      }
  }
  
  ```
  
  But since there is no controller or action for my homepage at the moment, I am not sure how I would go about doing this in Craft CMS.
  
  **Tags:** php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-21. Score: 1
  
  Try reading [this](https://docs.craftcms.com/v3/routing.html#advanced-routing).
  
  `return $this->render('index', ['twitterFeed' => $twitterFeed]);` is correct in craft 3 you just need to do some custom routing in order to reroute the homepage to your controller/action.
  
  > **Comments:**
  > **username_0**: Where do I put the controller file?  
  > **username_2**: It would have to be as a [Craft plugin](https://docs.craftcms.com/v3/plugin-intro.html) or as a Yii2 Module - you can generate the boilerplate code for both here: https://pluginfactory.io/  
  
  ---
  
  