php - 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');
});
解决方案
请分享完整的代码forms
我检查了你的,function
没有错误form
routes
我认为表格中的数据正在盯着,database
但redirecting
在商店抛出错误之后
我不确定,但可能是
所以尝试改变
从
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')
推荐阅读
- nginx - Nginx 服务器配置 - 访问文件夹
- .net - 在 Azure WebRole 中,当多个实例时 GetExternalLoginInfoAsync 始终为空
- reactjs - 当我提交联系表格时,它在反应 js 中显示错误
- java - java.lang.NoClassDefFoundError 当我将字体真棒添加到我的 fxml 文件时
- javascript - Vue路由器没有路由到指定位置
- python - 如何获得使用 Python 将一个坐标移动到另一个坐标所需的时间?
- javascript - Puppeteer`未定义的文本内容`
- redux - 从 ApolloClient onError 调度一个动作
- java - 使用显示/隐藏页面上另一个按钮的 PDFBox 2.0.5 创建 PDF 按钮。只有最后创建的集合才能正常工作
- javascript - bootstrap3 导航栏在 jsfiddle 上折叠但在本地浏览器上不起作用