5

I am using laravel 4.2

die(View::make('amendments.changesPopUp', $this->data));

This is the code that I am using to get the view for an ajax call. This is working for my local machine running on windows but this is not working for server( unix ). Any idea as to why this is hapening?

and yes the I have checked the lowercase and upper case, the cases for the filename matches. and the weird thing is the error points to the line 0 of the controller that it is using.

This is the error that I get

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Method Illuminate\\View\\View::__toString() must not throw an exception","file":"mysite.com/app/controllers/myController.php","line":0}}

Update: This worked when I used simple php file instead of a blade template. I still do not know what caused the error?

developernaren
  • 514
  • 5
  • 20
  • @TheShiftExchange because it is an ajax call and we have to print any repsonse in order to send over and die is to make sure I do no return anything after I echo the html, but that is not the question. – developernaren Sep 24 '14 at 07:07
  • Yes it is - probably reason you are getting the error is because you `die()` - which is being treated as throwing the exception. – Laurence Sep 24 '14 at 07:15
  • please see my update.. it works when i do not use blade template. and this is not the first time I used die.. it is working in my local machine.. please read the question carefully before commenting.. – developernaren Sep 24 '14 at 07:19
  • post your view file please – Laurence Sep 24 '14 at 07:19
  • 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 Jun 20 '16 at 14:42

2 Answers2

8

As mentioned already, don't use die() for other than debugging purposes.

Another thing to note is that because PHP's error handling for __toString implementations is really bad (no stack traces etc), use echo View::make(...)->render() instead of just echo View::make(...) to get more descriptive errors - though in your case you can replace echo with die.

But again, don't use die.

Andreas
  • 7,991
  • 2
  • 28
  • 37
  • 1
    Good tip for using the render() method. I ran into this error and switching to render() rather than __toString() I was able to locate the issue almost immediately. – Daniel Wood May 01 '15 at 16:13
1

You should never die() out an input. Laravel expects to handle the response, and you are short circuiting the framework by using die().

Your response should simply be

return View::make('amendments.changesPopUp', $this->data);

That will then only print the changesPopUp file - which will be correctly interrepted by the browser for the AJAX call.

Laurence
  • 58,936
  • 21
  • 171
  • 212
  • is there a reason that this would work on windows and not in unix? because this is perfectly working on my local machine, the issue is just in live server. Is there a reason that the laravel interpretation would differ basing on the os used. I understand the unicode may cause the error but I do not see that os interpreting the whole exception in differnt ways. – developernaren Sep 24 '14 at 07:26
  • I'm not sure - its hard to test. But two tips: 1. You should use Laravel Homestead, so that your 'development' environment is similar to the production environment, otherwise you run into issues like this from time to time. 2. You should never use die() in production on Laravel. – Laurence Sep 24 '14 at 07:32