Laravel中的路由问题和找不到页面

问题描述 投票:6回答:3

我在弹出窗口中有联系表单但是当我点击发送按钮时我找不到页面而不是将我重定向到主页。

这是我的路线

Route::post('/contact_us','HomeController@contact_us')->name('contact_us');

HomeController.php中的函数

public function contact_us(Request $request)
{ 
    $validator=Validator::make($request->all(), [
        'name' => 'required',
        'phone' => 'required',
        'email' => 'required|email'
    ]);

    if($validator->fails())
    {

      Session::flash('error', "join_us");
      return back()->withInput()->withErrors($validator, 'contact');
    }
    $data = array(
        'name' => $request->name,
        'email'  => $request->email,
        'phone'  => $request->phone,
        'created_at' => date('Y-m-d h:i:s'),
        ); 
    $email=$request->email;

     DB::table('application_from')->insert($data); 

    return Redirect::to('/')->with('message', 'Application added successfuly');
}    

表格打开和关闭标签

{{Form::open(array('route'=>'contact_us','method'=>'post'))}}

..... form inputs

{{Form::close()}}

当我点击提交时,我有

Not Found

The requested URL /contact_us was not found on this server.

为什么在重新加载主页时尝试加载此URL?

更新表格

<div class="modal-body">
    {{Form::open(array('route'=>'contact_us','method'=>'post'))}}
         <div class="form-group {{ $errors->contact->has('name') ? 'has-error' : '' }}">
              @if ($errors->contact->has('name'))
                  <span class="small text-danger ">
                      <b>{{ $errors->contact->first('name') }}</b>
                  </span>
              @endif
              <input type="text" name="name" class="form-control" placeholder="Name" >
         </div>
         <div class="form-group {{ $errors->contact->has('email') ? 'has-error' : '' }}">
              @if ($errors->contact->has('email'))
                  <span class="small text-danger ">
                     <b>{{ $errors->contact->first('email') }}</b>
                  </span>
              @endif
              <input type="text" name="email" class="form-control" placeholder="Email" >
         </div>
         <div class="form-group {{ $errors->contact->has('phone') ? 'has-error' : '' }}">
              @if ($errors->contact->has('phone'))
                   <span class="small text-danger ">
                        <b>{{ $errors->contact->first('phone') }}</b>
                   </span>
              @endif
              <input type="text" name="phone" class="form-control" placeholder="Phone" >
         </div>
         <div class="form-group text-center">
              <button type="submit" class="btn btn-custom btn-sm btn-block">Submit</button>
         </div> 
         {{Form::close()}}
 </div>

更新2:发送邮件功能

Mail::send('contact_us_email', $data, function($message) use ($email){
            $message->to($email)->subject('Site')->cc('[email protected]');
            $message->from('[email protected]');
});
php laravel laravel-5.6
3个回答
1
投票

回答您的电子邮件问题

尝试在.env文件中添加以下内容

# For Localhost Email
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

# For Hosting Email
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

这些凭据适用于基于gmail的电子邮件,请不要忘记执行以下步骤:

  1. 转到您的Google帐户。
  2. 在左侧导航面板中,单击“安全性”。
  3. 在页面底部的“不太安全的应用程序访问”面板中,单击“打开访问权限”。

1
投票

似乎你错过了表格中的csrf-token。在表单中添加csrf字段,如下所示:

<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />

要么

{!! Form::token() !!}

希望,这将解决您的问题。


1
投票

请分享forms的完整代码

我检查了你的functionformroutes没有错误

我认为表格中的数据是在database中盯着但是商店之后的redirecting正在抛出错误

我不是很确定,但可能是

所以尝试改变

 return Redirect::to('/')->with('message', 'Application added successfuly');

return redirect()->back()->with('message','Application added successfuly');

还有FROM

{{Form::open(array('route'=>'contact_us','method'=>'post'))}}

  {!! Form::open(['route' => ['contact_us']]) !!}
  @method('POST')
© www.soinside.com 2019 - 2024. All rights reserved.