htmlentities() 期望参数 1 是字符串,给定对象

Posted

技术标签:

【中文标题】htmlentities() 期望参数 1 是字符串,给定对象【英文标题】:htmlentities() expects parameter 1 to be string, object given 【发布时间】:2015-06-27 14:21:48 【问题描述】:

我目前正在使用 Laravel 5,但如有任何建议,我们将不胜感激。我目前正在使用 laravel 的表单生成器,并且不断收到此 html 实体错误。我什至尝试将 textarea 更改为文本字段,但无济于事。下面是我的完整堆栈跟踪。

堆栈跟踪

ErrorException in helpers.php line 455:
htmlentities() expects parameter 1 to be string, object given (View: /home/fujita/resources/views/emails/contact.blade.php)
in helpers.php line 455
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 43
at PhpEngine->evaluatePath('/home/fujita/storage/framework/views/e0d450cfe03e94c1d8bac332b837ce79', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => object(Message))) in CompilerEngine.php line 57
at CompilerEngine->get('/home/fujita/resources/views/emails/contact.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => object(Message))) in View.php line 136
at View->getContents() in View.php line 104
at View->renderContents() in View.php line 78
at View->render() in Mailer.php line 425
at Mailer->getView('emails.contact', array('name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => object(Message))) in Mailer.php line 288
at Mailer->addContent(object(Message), 'emails.contact', null, null, array('name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => object(Message))) in Mailer.php line 162
at Mailer->send('emails.contact', array('name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => 'jkjkj'), object(Closure)) in Facade.php line 216
at Facade::__callStatic('send', array('emails.contact', array('name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => 'jkjkj'), object(Closure))) in PagesController.php line 37
at Mail::send('emails.contact', array('name' => 'jkj', 'email' => 'jkjkjk@m.com', 'phone' => 'jkjkjk', 'message' => 'jkjkj'), object(Closure)) in PagesController.php line 37
at PagesController->store(object(ContactFormRequest))
at call_user_func_array(array(object(PagesController), 'store'), array(object(ContactFormRequest))) in Controller.php line 246
at Controller->callAction('store', array(object(ContactFormRequest))) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(PagesController), object(Route), 'store') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(PagesController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\PagesController', 'store') in Route.php line 198
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131
at Route->run(object(Request)) in Router.php line 691
at Router->Illuminate\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 693
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
at Kernel->Illuminate\Foundation\Http\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

contact.blade.php

<p>
Name:  $name 
</p>

<p>
 $email 
</p>

<p>
 $phone 
</p>

<p>
 $message 
</p>

ContactFormRequest.php

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class ContactFormRequest extends Request 


    public function authorize()
    
        return true;
    


    public function rules()
    
        return [
            'name' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
            'message' => 'required',
        ];
    


index.blade.php 上的表单

        !! Form::open(array('route' => 'contact_store', 'class' => 'form')) !!

        <div class="form-group">

            !! Form::text('name', null, 
                array('required', 
                      'class'=>'form-control', 
                      'placeholder'=>'Name*')) !!
        </div>

        <div class="form-group">

            !! Form::text('email', null, 
                array('required', 
                      'class'=>'form-control', 
                      'placeholder'=>'Email Address*')) !!
        </div>
        <div class="form-group">

            !! Form::text('phone', null, 
                array('required', 
                      'class'=>'form-control', 
                      'placeholder'=>'Phone')) !!
        </div>

        <div class="form-group">

            !! Form::textarea('message', null, 
                array('required', 
                      'class'=>'form-control', 
                      'placeholder'=>'Message')) !!
        </div>

        <div class="form-group">
            !! Form::submit('Submit', 
              array('class'=>'btn btn-primary')) !!
        </div>
        !! Form::close() !!
        <ul>
            @foreach($errors->all() as $error)
                <li> $error </li>
            @endforeach
        </ul>

页面控制器

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContactFormRequest;





class PagesController extends Controller 

    public function home()
    

        // Mail::send('emails.contact', array('name' => 'Justin' ), function($message)
        //  $message->to('justin@sfp.net', 'Justin')->subject('Test');
        // );
        return view('pages.index');
    

    public function store(ContactFormRequest $request)


    \Mail::send('emails.contact',
        array(
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'phone' => $request->get('phone'),
            'message' => $request->get('message')


        ), function($message)
    
        $message->from('justin@sfp.net');
        $message->to('justin@sfp.net', 'Admin')->subject('Chyall Pharma Contact');
    );

  return \Redirect::route('/')->with('message', 'Thanks for contacting us!');



【问题讨论】:

【参考方案1】:

你添加了吗

到composer.json:

“要求”: “照亮/html”:“5.0.*@dev” ,

到 app.php 提供者数组

'照亮\Html\HtmlServiceProvider',

到 app.php 别名数组

'Html' => '照亮\Html\HtmlFacade',

'Form' => 'Illuminate\Html\FormFacade',

然后

composer update

【讨论】:

是通过 cmd 提示符完成的,但是是的,这是我在 composer.json 中有 "illuminate/html": "~5.0" 的行,其他一切都如您所述。【参考方案2】:

问题出在PagesController里面的Mail::send

'message' => $request->get('message');

您正在使用变量名'message',应该避免使用它。

注意:$message 变量总是传递给电子邮件视图,并允许 附件的内联嵌入。所以,最好避免通过 视图负载中的消息变量。

来源:http://laravel.com/docs/5.0/mail#basic-usage 在第一个注释中。

您可能需要将变量名称更改为其他名称。

'bodyMessage' => $request->get('message');

别忘了更改contact.blade.php 中的变量名

<p>
Name:  $name 
</p>

<p>
 $email 
</p>

<p>
 $phone 
</p>

<p>
 $bodyMessage  // This line.
</p>

【讨论】:

谢谢。我摆脱了那个错误,但现在它说 Route [/] 未定义。我在索引页面上有我的联系表。有没有我不知道的更好的方法? 你可以用重定向方法替换它return redirect('/')-&gt;with('message', 'Thanks for contacting us!');链接laravel.com/docs/5.0/requests#obtaining-a-request-instance 酷所以它现在重定向但没有消息...有没有办法在屏幕上获取消息?它适用于错误 很好的答案 - 让我难过 如果有任何1,请在此处查看相同的问题...***.com/questions/38008979/…【参考方案3】:
!! Form::submit('Submit', array('class'=>'btn btn-primary')) !!

这行需要一个非数组的第二个参数,试试改成:

!! Form::submit('Submit', '', array('class'=>'btn btn-primary')) !!

【讨论】:

【参考方案4】:

当我使用错误的表单方法时,我也遇到了这个错误。要创建文本输入,请确保使用 text 方法,而不是 input 方法。

!! Form::text('name', null, ['class' => 'form-control']) !!

代替:

!! Form::input('name', null, ['class' => 'form-control']) !!

这已纠正错误。 这适用于带有Laravel Collective 的 Laravel 5.3(即 composer require "laravelcollective/html":"^5.3.0")

【讨论】:

【参考方案5】:

如果你想打印一个数组,必须这样做:

<?php dd($nameOfvarArray); ?>

参考:https://laravel.com/docs/5.2/helpers#method-dd

【讨论】:

以上是关于htmlentities() 期望参数 1 是字符串,给定对象的主要内容,如果未能解决你的问题,请参考以下文章

helpers.php 第 531 行中的 ErrorException:htmlentities() 期望参数 1 为字符串,给定数组

有啥 绕过 htmlentities 进行 xss 攻击的方法吗

PHP htmlentities 和 htmlspecialchars的区别

ErrorException strtotime() 期望参数 1 是字符串,给定数组

153-PHP htmlentities函数

php pathinfo() 期望参数 1 是字符串,数组在 [重复]