4

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?

Alex
  • 8,461
  • 6
  • 37
  • 49
user3150060
  • 1,725
  • 7
  • 26
  • 46
  • There's an error in one of your views. – ceejayoz Feb 04 '15 at 19:05
  • wrapper.php is a views file , but which one you mean like the header ? – user3150060 Feb 04 '15 at 19:06
  • It is either in `layouts/blocks/header` or `layouts/blocks/footer`. Comment out one or the other to pinpoint which view is having the issue, then post the contents of the view here so people can help. – patricus Feb 04 '15 at 19:09
  • Yes I know I am having an issue I have an old script using Laravel 3.2 and I am trying to move it to Laravel 4.2 its a nightmare , They user Asset::styles(); in header file I think I need to replace it with laravekl 4.2 equivalent not sure how? – user3150060 Feb 04 '15 at 19:14
  • http://yetanotherprogrammingblog.com/content/upgrading-from-laravel-3-to-laravel-4 – ceejayoz Feb 04 '15 at 19:15
  • Great but I don't see what he did with Asset::styles(); ? – user3150060 Feb 04 '15 at 19:26
  • Laravel 4 doesn't have a built in asset manager. You'll either need to install a new package and update the code to use it, or just manually write the assets into the view. You may want to check out this post: [http://stackoverflow.com/questions/14205404/laravel-4-what-is-the-replacement-for-assetadd](http://stackoverflow.com/questions/14205404/laravel-4-what-is-the-replacement-for-assetadd). – patricus Feb 04 '15 at 19:28
  • Possible duplicate of [Laravel Error: Method Illuminate\View\View::\_\_toString() must not throw an exception](http://stackoverflow.com/questions/26534016/laravel-error-method-illuminate-view-view-tostring-must-not-throw-an-excep) – Mārtiņš Briedis Mar 01 '16 at 06:37

1 Answers1

5

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.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • I put this in my routes.php still same error and no extra error details I can't figure whats the actual issue – user3150060 Feb 04 '15 at 20:30
  • @user3150060 That means the error's happening in one of your sub views. Comment out the View::make lines to see if it's one, or both of the lines. Then open the template for the `layouts/blocks/header` or the `layouts/blocks/footer` -- look for PHP code in there and repeat until you've tracked down the problem. – Alana Storm Feb 04 '15 at 20:49
  • 3
    I noticed that adding ->render(); displayed more errors and I found out whats wrong – user3150060 Feb 04 '15 at 21:22
  • @user3150060 I'm not 100% sure, but I bet the View's `__toString` method definition looks something like `$this->render(...)` By explicitly rendering the view you skipped the `__toString` step and let PHP tell you what the error was. Good thinking! – Alana Storm Feb 04 '15 at 21:23