Response内容必须是实现__toString(),\"boolean \"的字符串或对象."

Ale*_*len 23 php laravel laravel-4

我正在尝试使用渲染的View返回Response::json但是我收到此错误:

The Response content must be a string or object implementing __toString(), \"boolean\" given."
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

$posts = Post::where( ... )->orderBy( ... )->get();
$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();
$data['msg'] = "ok";

return Response::json($data);
Run Code Online (Sandbox Code Playgroud)

如果var_dump($data)我得到这个:

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=2)</i>
  'posts' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'&lt;div class=&quot;post postGrid&quot; data-id=&quot;1864&quot;&gt;&#10; &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;img src=&quot;http://objavi.net/&quot; id=&quot;imgWrap&quot; data-original=&quot;/thumbs/YAo4wFzIpl76.jpg&quot; class=&quot;lazy&quot; alt=&quot;Deset manje poznatih ?injenica o Jozefu Staljinu&quot;&gt;&lt;/a&gt;&#10;  &#10;   &lt;div id=&quot;bodyPreview&quot;&gt;&#10;     &#10;       &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;h1 class=&quot;previewTitle&quot;&gt;Deset manje poznatih ?injenica o Jozefu Staljinu&lt;/h1&gt;&lt;/a&gt;&#10;&#10;     &lt;h3 id=&quot;postInfo&quot;&gt;&#10;                         &lt;a class=&quot;paint&quot; href=&quot;/category/17&quot;&gt;zanimljivosti&lt;/a&gt;&#10; '...</font> <i>(length=12172)</i>
  'msg' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'ok'</font> <i>(length=2)</i>
</pre>
Run Code Online (Sandbox Code Playgroud)

这是posts.partials.loadHome观点:

@foreach($posts as $post)

<div class="post postGrid" data-id="{{ $post->id }}">
    <a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}">{{ HTML::image(null, $post->title, ["id" => "imgWrap", "data-original" => $post->getThumb(), "class" => "lazy"]) }}</a>

    <div id="bodyPreview">

        <a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}"><h1 class="previewTitle">{{ e($post->title) }}</h1></a>

        <h3 id="postInfo">
            @foreach($post->categories as $c)
                <a class="paint" href="/category/{{ $c->id }}">{{ $c->name }}</a>
            @endforeach
        </h3>

        <h2 class="bodyPreview">{{ strip_tags(truncString($post->body, 160)) }}</h2>

        <div id="createdBy">
            <a href="{{ URL::action('UsersController@show', $post->user()->first()->id) }}">
                {{ HTML::image($post->user()->first()->getAvatar(), $post->user()->first()->username, ["width" => "32", "height" => "32"]) }}

                {{{ $post->user()->first()->username }}}
            </a>
            <label id="timeAgo">{{ localDate($post->created_at); }}</label>
        </div>
    </div>
</div>
@endforeach
Run Code Online (Sandbox Code Playgroud)

我测试了这个在本地主机上,一切工作正常.可能是什么问题呢?

小智 16

检查以确保没有任何非法字符.我有这个问题曾经和字符串运行函数utf8_encode和解决的问题.

  • 这在Laravel中并不是很实用.你如何在一个对象上运行`utf8_encode`? (2认同)

Wil*_*urb 13

我跑至本博客文章,并认为它给固定它一个不错的主意:

如果您要调试它,这种错误会杀死您或者逐步跟踪它,您将永远找不到解决方案因为此错误发生在响应中,我的意思是只有在响应准备就绪后它才会被框架检测到要呈现,因此,它是作为消息表示"反应是布尔".通常它会在某些变量影响响应内容的看法发生.只需逐个检查视图变量,然后删除它们中的每个尝试再次运行.你会发现,造成这种错误的变量.但在进入此路径之前,请尝试通过任何其他视图页面(刀片模板)更改视图,并查看错误是否仍然存在.如果不是,那么在视图页面的问题.


小智 9

创建以下功能

function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            self::utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            self::utf8_encode_deep($input->$var);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试执行以下操作

utf8_encode_deep($data);
return Response::json($data);
Run Code Online (Sandbox Code Playgroud)


Ráp*_*rás 0

您没有创建$data = array();并且本地和远程 PHP 版本不同。