Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ViewModel parameters
  • I have database queries in my ViewModel that require dynamic parameters, such as a Post ID, date range, etc. How do I pass in these values from the controller so I can properly build my query? Imagine something simple like this:
    // controller/posts.php
    ...
    public function action_show()
    {
      $this->template->content = ViewModel::forge('posts/show');
    }
    ...
    
    // view/posts/show.php
    ...
    public function view()
    {
      $this->post = Model_Post::find(x);
    }
    ...
    

    I'm not clear on how I would get the Post ID in this example.
  • Depends on where you're getting it from. If it's GET/POST/etc I'd just use the Input::get()/Input::post()/Input::param() method in my viewmodel. If it's a segment from the URI it's better practice to determine it in the controller and pass it to the ViewModel:
    // controller/posts.php
    ...
    public function action_show($id)
    {
        $this->template->content = ViewModel::forge('posts/show');
        $this->template->content->post_id = $id;
    }
    

    After which it's available as a property within the ViewModel:
    // view/posts/show.php
    ...
    public function view()
    {
      $this->post = Model_Post::find($this->post_id);
    }
    
  • Cool, thanks!

Howdy, Stranger!

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

In this Discussion