<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>

CC BY-SA 4.0
2018-06-21T16:04:13.773Z
Where do I put the controller file?
MAX POWER
CC BY-SA 4.0
2018-06-21T18:46:33.067Z
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/
Brad Bell
CC BY-SA 4.0
2018-06-21T14:50:55.107Z
2018-06-21T14:50:55.107Z
dustfeather
<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>

<php>
Pass custom variable to view
# 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:
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/  

---

