Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Acessing Viewmodel from Controller
  • How to forge Viewmodel  from Controller? I have ErrorException [Fatal Error ]: Class 'Viewmodel' not found

  • where to use this command Viewmodel::forge('index');
  • Is your controller in a namespace? If so you need to prefix it with a \ to force a load from the global namespace.

    So \Viewmodel::forge()...
  • Controller:
    class Controller_Index extends Controller
    {

        public function action_index()
        {
            return Viewmodel::forge('index');
        }
    }
    Viewmodel:
    class View_Index extends Viewmodel
    {

        public function view()
        {
            $this->title = 'Testing this Viewmodel thing';

            $this->articles = Model_Articles::find('all');
        }
    }
    View:
    <h1><?php echo $title; ?></h1>

    <ul>
    <?php
        foreach ($articles as $a)
        {
            echo '<li>'.$a->title.'</li>';
        }
    ?>
    </ul>

  • ViewModel, not Viewmodel.
  • I said before that the documentation is puzzle for me.
  • How to use Viewmodel with code?
  • I gave you an example a view posts back?
  • I need to check where the class name issue comes from.

    Fuel standard is that classnames are ucfirst(), so the docs are correct. But I see the welcome controller use ViewModel, which is odd...
  • HarroHarro
    Accepted Answer
    Was a bug in the autoloader, I have just fixed it in 1.8/develop. If you're on 1.7.x, you can just backport it from the repository.
  • Thank you, it works but the FuelPhp community is going to prosper if you or someone repair the documentation. 
  • Docs are correct, the standard is ucfirst(), so it is "Viewmodel", not "ViewModel".

    The problem was that:
    a) the class itself is wrongly named ViewModel (for historic reasons, and never corrected)
    b) the autoloader was case-sensitive, which is wrong, because class names in PHP are not. Which is now fixed, so as of 1.7.2 this problem no longer exists.

Howdy, Stranger!

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

In this Discussion