首页 > 解决方案 > Laravel 中的路由问题和找不到页面

问题描述

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

这是我的路线

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('info@example.com');
            $message->from('info@example.com');
});

标签: phplaravellaravel-5.6

解决方案


请分享完整的代码forms

我检查了你的,function没有错误formroutes

我认为表格中的数据正在盯着,databaseredirecting在商店抛出错误之后

不确定,但可能是

所以尝试改变

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

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

还有 来自

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

  {!! Form::open(['route' => ['contact_us']]) !!}
  @method('POST')

推荐阅读