user3150060
user3150060

Reputation: 1745

Laravel Error : Method Illuminate\View\View::__toString() must not throw an exception

I am using larval 4.2 and I am getting the following error in my wrapper.php my view file :

   <?php echo View::make('layouts/blocks/header')->with('sidebar', $sidebar)->with('active', $active); ?>
   <?php echo $content; ?>
   <?php echo View::make('layouts/blocks/footer'); ?>

Error:

   Error : Method Illuminate\View\View::__toString() must not throw an exception

Do you know whats causing this?

Upvotes: 4

Views: 7682

Answers (1)

Alana Storm
Alana Storm

Reputation: 166066

Laravel renders its views by casting an Illuminate\View\View object as a string. If an object is cast as a string and has a __toString method set, PHP will call the __toString method and use that value from that as the cast value.

For example, this program

class Foo
{
    public function __toString()
    {
        return 'I am a foo object';
    }
}
$o = new Foo;
echo (string) $o;

will output

I am a foo object.

There's a big caveat to this behavior -- due to a PHP implmentation detail, you can't throw an exception in __toString.

So, it looks like the problem you're having is something in your view does throw an exception. Based on the information you've provided, the error could be anything. The way I'd debug this further is to try running the PHP code in your view

echo View::make('layouts/blocks/header')->with('sidebar', $sidebar)->with('active', $active);
echo $content;
echo View::make('layouts/blocks/footer');

outside of a view (a route, a controller action, etc), making sure $sidebar, $content, etc have the same values. This should still throw an exception, but because it's outside of __toString PHP will give you more information on why it threw an exception. With a real error message you'll be able to address the actual problem.

Upvotes: 5

Related Questions