laravel 致命错误:发送表单时找不到类“App\Http\Controllers\Input”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37202978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:47:28  来源:igfitidea点击:

Fatal error: Class 'App\Http\Controllers\Input' not found when sending a form

phphtmltwitter-bootstraplaravel

提问by user2813209

I'm trying to send an email filled with a form from a Laravel app.

我正在尝试发送一封电子邮件,其中包含来自 Laravel 应用程序的表单。

When you hit submit it throws the above error:

当您点击提交时,它会引发上述错误:

Fatal error: Class 'App\Http\Controllers\Input' not found

致命错误:找不到类“App\Http\Controllers\Input”

Not sure why as I don't have, nor knew I needed to have an Input controller, or what I would put in it.

不知道为什么,因为我没有,也不知道我需要一个输入控制器,或者我会在里面放什么。

Below is the content of the controller:

下面是控制器的内容:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class contact extends Controller
{
     // This function will show the view
    public function showForm()
    {
        return view('pages.contact');
    }

     public function handleFormPost()
     {
         $input = Input::only('name', 'email', 'msg');

         $validator = Validator::make($input,
             array(
                 'name' => 'required',
                 'email' => 'required|email',
                 'msg' => 'required',
             )
         );

         if ($validator->fails())
         {
             return Redirect::to('contact')->with('errors', $validator->messages());
         } else { // the validation has not failed, it has passed


            // Send the email with the contactemail view, the user input
            Mail::send('contactemail', $input, function($message)
            {
                 $message->from('[email protected]', 'Your Name');

                 $message->to('[email protected]');
             });

             // Specify a route to go to after the message is sent to provide the user feedback
             return Redirect::to('thanks');
         }

     }
 }

Below is the view of the forum (based on bootstrap):

以下是论坛的观点(基于bootstrap):

<div class="container">
    <h1>A basic contact form</h1>
    <form id="contact" method="post" class="form" role="form">

        @if(Session::has('errors'))
            <div class="alert alert-warning">
                @foreach(Session::get('errors')->all() as $error_message)
                    <p>{{ $error_message }}</p>
                @endforeach
            </div>
        @endif

        <div class="row">
            <div class="col-xs-6 col-md-6 form-group">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input class="form-control" id="name" name="name" placeholder="Name" type="text"autofocus="">
            </div>
            <div class="col-xs-6 col-md-6 form-group">
                <input class="form-control" id="email" name="email" placeholder="Email" type="text">
            </div>
        </div>
        <textarea class="form-control" id="message" name="msg" placeholder="Message" rows="5"></textarea>
        <br>
        <div class="row">
            <div class="col-xs-12 col-md-12 form-group">
                <button class="btn btn-primary pull-right" type="submit">Submit</button>
            </div>
        </div>
    </form>
</div>

回答by Y. Joy Ch. Singha

Used this in your contact.php Controllers-

在您的 contact.php 控制器中使用它-

use Illuminate\Support\Facades\Input;

your error will get fixed. Thanks.

您的错误将得到修复。谢谢。

回答by lewis4u

Input:: is replaced with Request::. Instead of

Input:: 替换为 Request::。代替

$input = Input::only('name', 'email', 'msg');

use this:

用这个:

$input = Request::only('name', 'email', 'msg');

And if you get error something about 'should not use statically' just add this at the top of your file

如果您收到有关“不应静态使用”的错误信息,只需将其添加到文件顶部

use Request;

If you already have this line:

如果您已经有了这一行:

use Illuminate\Http\Request;

delete it because you can't have two classes with the same name in one file

删除它,因为一个文件中不能有两个同名的类

回答by Esakki Krishnan

public function handleFormPost(Request $request)
{
    $name = $request->get('name');
    $email = $request->get('email');
    $msg = $request->get('msg');   
}

OR

或者

public function handleFormPost(Request $request)
{
    $input = $request->all();   
}

回答by D Coder

you not use input try this :

你不使用输入试试这个:

use Input;

Put it after the namespace declaration like this

像这样把它放在命名空间声明之后

<?php 

namespace App\Http\Controllers; 
use Input;
...

?>