【问题标题】:"htmlentities() expects parameter 1 to be string, array given" in PHP“htmlentities() 期望参数 1 是字符串,给定数组”在 PHP
【发布时间】:2017-03-10 06:02:52
【问题描述】:

我的问题似乎与 Laravel 助手有关。一切正常,直到我将一些文件更改为其他文件夹,当然我更改了路由以及路由和控制器,但现在我收到此消息:

HtmlBuilder.php 第 65 行中的 ErrorException: htmlentities() 期望参数 1 是字符串,给定数组(查看:/Library/WebServer/Documents/gamstec/resources/views/posts/create.blade.php)

这是我的创建文件:

<div class="content">
    <div class="container-fluid">
        <div class="row well bs-component">
            <div class="col-md-8 col-md-offset">
            <div class="input-group">

                <h3 class="text-center">Crear Nueva Publicación</h3>
                <hr>

                {!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!}

                    {{ Form::label('title', ' Título:', array('class' => 'fa fa-pencil')) }}

                    {{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}

                    {{ Form::label('body', ' Contenido:', array('class' => 'fa fa-pencil-square-o')) }}

                    {{ Form::textarea('body', null, array('class' => 'form-control', 'required' => '')) }}

                    {{ Form::submit('Publicar', array('class' => 'btn btn-info')) }}


                </div>
            </div> <!-- col-md-8 end -->


            <div class="col-md-4">
            <div class="input-group">
                <h3 class="text-center">Categoría e imágen</h3>
                <hr>


                     <br> <hr>

                    {{ Form::label('badge', ' Etiqueta:', array('class' => 'fa fa-tags')) }}

                    {{ Form::text('badge', array('class' => 'form-control', 'maxlength' => '20')) }}
                    <br> <hr>

                     {{ Form::label('postimg', ' Seleccionar Imagen', array('class' => 'fa fa-picture-o')) }}
                    <br> <br>
                    {{ Form::file('postimg', array('require' => '', 'maxlength' => '255')) }}

                {!! Form::close() !!}

            </div> <!-- item-group end -->
            </div> <!-- col-md-4 end -->


        </div> <!-- end of row -->
    </div> <!-- end of container -->
</div> <!-- end of content -->

这是我的控制器文件:

<?php


public function index()
{
    return view('admin');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('posts.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    // validate the data
    $this->validate($request, array(
        'title' => 'required|max:255',
        'body' => 'required',
        ));

    // store in database
    $post = new Post;

    $post->title = $request->title;
    $post->body = $request->body;
    $post->badge = $request->badge;
    $post->postimg = $request->postimg;

    $post->save();

    Session::flash('success', 'La publicación se ha creado correctamente');

    // redirect to another page
    return redirect()->route('show', $post->id);
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $post = Post::find($id);
    return view('show')->withPost($post);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

感谢您的帮助。

【问题讨论】:

  • 您没有显示HtmlBuilder.php line 65,但那里有对htmlentities() 的调用,并且您正在向其传递一个数组。
  • 抱歉,这是第 65 行的 HtmlBuilder.php public function escapeAll($value) { return htmlentities($value, ENT_QUOTES, 'UTF-8'); }
  • 所以你必须追踪它。您的代码中的某些内容调用了最终调用 escapeAll() 的其他内容。在htmlentities() 之前使用debug_print_backtrace() 来查看它的来源。
  • 我是新手,你能告诉我如何使用 debug_print_backtrace() 吗?
  • 对不起,系统检测到的参数为空。终于运行了:D

标签: php laravel blade


【解决方案1】:

那是产生错误的那一行:

{{ Form::text('badge', array('class' => 'form-control', 'maxlength' => '20')) }}

您可以传递附加值选项,因为第二个参数不能是数组

要添加其他属性,请将第三个参数传递给该方法。第三个参数必须是一个数组。

改成这样:

{{ Form::text('badge', '', array('class' => 'form-control', 'maxlength' => '20')) }}

【讨论】:

  • 如果表单出现错误,请尝试使用 old('badge') 而不是 '' 来预填值
  • @JuanRincón 如果此答案解决了您的问题,请考虑通过单击向下箭头下方勾勒出的复选标记来接受它;这会将问题标记为“已解决”,并奖励花时间解决问题的回答者。
【解决方案2】:

这是您代码中的这一行:

{{ Form::text('badge', array('class' => 'form-control', 'maxlength' => '20')) }}

它需要 null 作为第二个值:

{{ Form::text('badge', null, array('class' => 'form-control', 'maxlength' => '20')) }}

【讨论】:

    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2017-05-17
    • 2017-09-23
    • 1970-01-01
    • 2016-10-26
    • 2015-06-27
    相关资源
    最近更新 更多