Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Purpose of before() and after() methods in template controllers
  • What are the before() and after() methods for in template controllers? E.g. practical use cases?

    The docs don't actually state the purpose, only provide example code with a comment saying: //do stuff.

    My design intent is:

    within views/template.php
      - simple markup for page, header and footer
      - call view::forge() to render partials (e.g. things like navigation widgets)
      - echo $content

    within views/page.php
      - markup for everything that should appear in $content

    Am I doing it wrong? 
  • HarroHarro
    Accepted Answer
    Nothing. There is no requirement for your controllers to have before() and after() methods.

    They are present in the base controller (in this case Controller_Template), where before() will make sure that $this->template contains a View object for your action method to work with, and after() will make sure that if your action method does not return a Response object, the returned value will be wrapped in one.

    In general: before() can contain code that has to be executed for every action method called in your controller. A typical example is some template prepping (for example set a title or navigation, a sidebar perhaps), or access control. Similarly, an after() method is called after the action method is finished, again for some generic code.

    If you don't have a use for them, don't include them. If you use them, make sure you call the parent otherwise the controller will no longer work properly.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion