Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ViewModel get data from Controller
  • How to get data(variables) from ViewModel and how to access it(them), sent by Controller.
    Controller:
    class Controller_Index extends Controller
    {
        public function action_index()
        {
    $data['title'] = 'Home';
    $data['userInfo'] = 'Tom 50 active';
            return ViewModel::forge('index', $data);
        }
    }
    ViewModel:
    class View_Index extends Viewmodel
    {
        public function view()
        {
            $this->title = 'Testing this Viewmodel thing';
        }
    }

  • HarroHarro
    Accepted Answer
    With regards to data the API of the Viewmodel class is the same as for the View, so you can use the set(), set_safe() and get() methods to send data to it, or get data from it. Or use bind() to bind a variable by reference. And it also supports the global variants.

    So you can do

    return ViewModel::forge('index')->set('title', 'This is a title');

    which inside the Viewmodel becomes $this->title, and in the view wil be $title.

Howdy, Stranger!

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

In this Discussion