5

When developing a plugin, I am registering routes to controller actions. Other frameworks, read that as Laravel, typically return the controllers response.

public function showProfile($id)
{
    $user = User::find($id);

    return View::make('user.profile', array('user' => $user));
}

However, in Craft to render a template from your controller you would use the following.

public function actionPluginIndex()
{
   $this->renderTemplate('pluginName/template');
}

At the same time, you can also return the rendered template this way and it functions as expected.

public function actionPluginIndex()
{
   return $this->renderTemplate('pluginName/template);
}

Looking at craft/app/controllers/BaseController.php templateRender method shows that it accepts a parameter return.

So the real question is to return or not to return and I was hoping for a more technical explanation on the way Craft handles this.

5

There’s no reason for your controller to return anything – it will never be checked.

Many requests call more than one controller action, so it would be pretty presumptuous for Craft to think that it should actually output whatever your controller returns, and end the request.

Instead, it’s up to the actual controller to do the outputting, and end the request, if that’s what it wants to do.

$this->renderTemplate() comes from BaseController, and it’s basically a wrapper for craft()->templates->render(), except that after calling that, it will also set the Content-Type header for the request, insert any queued-up JS and CSS, and then end the request.

  • 2
    Brandon wins! I'll send you a free copy of my book, when I eventually write it. ;) – Lindsey D Jun 26 '14 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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