1

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.

MAX POWER
  • 111
  • 1

1 Answers1

1

Try reading this.

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.

dustfeather
  • 105
  • 6