Laravel error: Illuminate \ View \ View :: __ toString () method should not throw an exception

I am using larval 4.2 and I get the following error in my wrapper.php 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 what this causes?

+4
source share
1 answer

Laravel displays its views by exposing the object Illuminate\View\Viewas a string. If the object is passed as a string and has a method __toString, PHP will call the method __toStringand use this value from it as the value of the cast.

For example, this program

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

displays

I am a foo object.

There's a big caveat to this behavior - due to the granularity of the PHP implementation, you cannot throw an exception in __toString.

, , , , - - . , . , , - PHP-

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

(, ..), , $sidebar, $content .. . , __toString PHP , , . .

+5

All Articles