Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

php - Laravel Error: Method IlluminateViewView::__toString() must not throw an exception

Have you seen this lovely error while working in Laravel?

Method IlluminateViewView::__toString() must not throw an exception

I have seen it and it's incredibly annoying. I have found out two reasons why this error gets thrown. I just want to help people not take hours and hours of time.

View answers & situations below. :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a very simple solution: don't cast View object to a string.

Don't: echo View::make('..'); or echo view('..');

Do: echo View::make('..')->render(); or echo view('..')->render();

For PHP version <7.4 By casting view, it uses __toString() method automatically, which cannot throw an exception. If you call render() manually, exceptions are handled normally. This is the case if there is an error in the view - laravel throws an exception.

It's fixed in PHP >=7.4 you should not encounter this issue: https://wiki.php.net/rfc/tostring_exceptions.

For PHP version <7.4: This actually is a PHP limitation, not Laravels. Read more about this "feature" here: https://bugs.php.net/bug.php?id=53648


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...