Action Views Example

The default model binding example shows how models are automatically bound to controller actions. Another part of the framework's opinionated directory structure is how view files are associated with particular actions. For any controller action, there can be a view file located inside the application/views/templates/ directory.

Below the ExampleController has been shortened for the purposes of this example.

<?php
    class ExampleController extends FrontController
    {
        /**
         * URL: Example/articles/?name=&year=&month=
         *
         * @param string             $name a dummy holder for name of fake article
         * @param string             $year a dummy holder for year of fake article
         * @param string             $month a dummy holder for month of fake article
         * @return View
         */
        public function articlesAction($name = "", $year = "", $month = "")
        {
            $this->view->title = "Articles Route Test";
            $this->view->content = "Hello World from the <b>ExampleController articlesAction</b> action<br /><br />";

            $this->view->myBoundParameters = array(
                $name, $year, $month
            );

            return $this->view();
        }

        /**
         * URL: Example/helloWorld/
         *
         * @return View
         */
        public function helloWorldAction()
        {
            $this->view->title = "Hello World";
            $this->view->whoAmI = $this->model->whatIsMyName();

            $this->view->renderer("Example", "hello");

            return $this->view();
        }
    } /*end of class ExampleController*/

Below is the content of the application/views/templates/Example/articles.php file which is associated with the ExampleController, articlesAction().

<?php
    echo $this->content;
    echo "<br />";
    echo implode("<br />", $this->myBoundParameters);

Below is the content of the application/views/templates/Example/hello.php file which is associated with the helloWorldAction(). However from the example provided, we can see how the $this->view->renderer("Example", "hello"); line changes the default view file of an action.

<h4><?php echo $this->title;?> from <?php echo $this->whoAmI;?></h4>

One More Thing

Just look again at how the helloWorldAction() declares $this->view->whoAmI; and how it's displayed by the hello.php template.

Did you know that because of dynamically created variables any variable declared with $this->view->anyVariableName; is available within the view template as $this->anyVariableName;?