簡體   English   中英

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代碼

我檢查了你的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')

似乎你錯過了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的電子郵件,請不要忘記執行以下步驟:

  1. 轉到您的Google帳戶。
  2. 在左側導航面板中,單擊“安全性”。
  3. 在頁面底部的“不太安全的應用程序訪問”面板中,單擊“打開訪問權限”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM