Laravel 中的路由问题和找不到页面
Routes problem in Laravel and Page not found
我在弹出窗口中有联系表格,但是当我点击发送按钮时,我没有找到页面,而不是将我重定向到主页。
这是我的路线
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
抛出错误
I am not daam sure but may be
所以尝试改变
来自
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')
您似乎缺少表单中的 csrf-token
。
在表单中添加csrf字段如下:
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
或
{!! Form::token() !!}
希望这能解决您的问题。
回答您的电子邮件问题
尝试在您的 .env 文件中添加以下内容
# For Localhost Email
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
# For Hosting Email
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
这些凭据用于基于 gmail 的电子邮件,请不要忘记执行以下步骤:
- 转到您的 Google 帐户。
- 在左侧导航面板上,单击“安全”。
- 在页面底部的安全性较低的应用程序访问面板中,点击打开访问权限。
我在弹出窗口中有联系表格,但是当我点击发送按钮时,我没有找到页面,而不是将我重定向到主页。
这是我的路线
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
抛出错误
I am not daam sure but may be
所以尝试改变
来自
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')
您似乎缺少表单中的 csrf-token
。
在表单中添加csrf字段如下:
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
或
{!! Form::token() !!}
希望这能解决您的问题。
回答您的电子邮件问题
尝试在您的 .env 文件中添加以下内容
# For Localhost Email
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
# For Hosting Email
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
这些凭据用于基于 gmail 的电子邮件,请不要忘记执行以下步骤:
- 转到您的 Google 帐户。
- 在左侧导航面板上,单击“安全”。
- 在页面底部的安全性较低的应用程序访问面板中,点击打开访问权限。