Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Where does the Response go when using Controller_Template, ViewModels and Smarty?
  • Hello, I'm trying to figure out the usage of a combination of Controller_Template, ViewModels and Smarty. Everything is working as I want it but I get a logged error on every request: Warning - 2012-05-15 09:38:08 --> Fuel\Core\Request::execute - The Controller_Home::after() method should accept and return the Controller's response, empty return for the after() method is deprecated.
    Warning - 2012-05-15 09:38:08 --> Fuel\Core\Request::execute - The Controller_Home controller should return a string or a Response object, support for the $controller->response object is deprecated. I've read the examples showing the return of a view or a forged viewmodel, but I can't seem to figure out where my return should come from? Could someone let me know if I'm understanding this the right way, and what do I need to do to get rid of that pesky error?
    classes/controller/base.php:
    ----------
    abstract class Controller_Base extends Controller_Template
    {
     public $template = 'template.smarty';
    
     public function before()
     {
      parent::before();
     }
     
     public function after($response)
     {
      parent::after($response);
      
      // Set ViewModel
      $active = Request::active();
      $viewmodel = $active->route->segments[0] .'/'. $active->route->segments[1];
      View::set_global('content', ViewModel::forge($viewmodel));
     }
    }
    ----------
    
    classes/controller/home.php:
    ----------
    class Controller_Home extends Controller_Base
    {
     public function before()
     {
      parent::before();
     }
    
     public function action_index()
     {
      
     }
    }
    ----------
    
    classes/views/home/index.php:
    ----------
    class View_Home_Index extends ViewModel
    {
     protected $_view = 'home/index.smarty';
     
     public function before($data = null)
     {
      parent::before();
      
      View::set_global('title', 'Hello world') ;
     }
     
     public function view()
     {
      $this->hello = 'Hello world';
     }
    }
    ----------
    
    views/template.tpl:
    ----------
    <!DOCTYPE html>
    <html lang="en">
    <head>
     <title>{$title}</title>
    </head>
    <body>
     {$content}
    </body>
    </html>
    ----------
     
    views/home/index.tpl:
    ----------
    {$hello}
    ----------
    
    Many thanks,
    /Andy PS. What's wrong with the styling buttons?!
  • In FuelPHP 1.2+, Controller actions MUST return either a Response object, or a value that can be cast to string, as the result of the action. FuelPHP 1.1 doesn't throw an error on this, but will log the warning. Alternatively, your controller can have an after() method, that captures the return value of your action (if present) and can do some post processing. In this case, the after() method must return the Response action or a value that can be cast to string. The default after() method as it is present in our base controllers (like Controller_Template) will do that for you. However, looking at your code, your after() method calls the parent first, discards the result, and doesn't return anything. Since your after doesn't modify the response but only sets some global data, the best option would probably to move the call to the parent::after() to the end of your after() method, and return it:
    public function after($response)
    {
        // Set ViewModel
        $active = Request::active();
        $viewmodel = $active->route->segments[0] .'/'. $active->route->segments[1];
        View::set_global('content', ViewModel::forge($viewmodel));
    
        return parent::after($response);
    }
    

Howdy, Stranger!

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

In this Discussion