【问题标题】:Routes problem in Laravel and Page not foundLaravel 中的路由问题和找不到页面
【发布时间】:2019-03-15 07:20:07
【问题描述】:

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

这是我的路线

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');
});

【问题讨论】:

  • 请出示完整的表格。
  • 你试过使用Route::post而不使用/吗?
  • 类似这样的东西:Route::post('contact_us','HomeController@contact_us')-&gt;name('contact_us');
  • 更新为完整形式
  • @GugaNemsitsveridze,我试过没有/ -> 找不到

标签: php laravel laravel-5.6


【解决方案1】:

回答您的电子邮件问题

尝试在您的 .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 的电子邮件,不要忘记执行以下步骤:

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

【讨论】:

  • 我想我发现了部分问题。因为实际上contact_us 没有这样的页面,我已经将路由更改为Route::post('/','HomeController@contact_us')-&gt;name('contact_us');。现在页面重新加载正常,但仍然没有收到电子邮件。
  • 如果您仍想在表单中保留操作属性的内容,请将其更改为 Route::post('/contact_us','HomeController@contact_us')->name('contact_us')
  • 不,这不是问题 url 实际上是什么。发送电子邮件功能是
  • 我可以看看您发送电子邮件的代码吗?它不包含在问题中
  • 我已经编辑了我的答案以适合您关于电子邮件的问题
【解决方案2】:

您似乎缺少表单中的csrf-token。 以如下形式添加csrf字段:

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

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

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

【讨论】:

  • 嘿伙计,如果在HttpRequest 中找不到_token,那不是问题,它会抛出419Sorry Your session has expired message
  • 谢谢,但令牌不是问题,是的,它会抛出 url not found
  • @Manojkiran.A 是的,它会抛出 419,但最近在一个项目中进行了一些定制,并且由于令牌而抛出 404。
  • @Ivanov 当您提交表单或成功提交后重定向时出现问题?
  • @KamalPaliwal,我不知道你是如何区分这两个动作的。我的意思是我正在单击Submit 按钮,我看到新页面site.com/contact_us 在服务器上找不到。
【解决方案3】:

请分享forms的完整代码

我检查了你的functionformroutes 没有错误

我认为表单中的数据盯着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')

【讨论】:

  • 两个都试了还是没找到。
  • 我也试过不将其存储到数据库中,例如评论了存储它的行 - 仍然相同。
猜你喜欢
  • 2019-09-02
  • 2022-07-18
  • 2013-06-26
  • 2015-11-22
  • 2016-10-20
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
相关资源
最近更新 更多