简体   繁体   English

Laravel中的路由问题和找不到页面

[英]Routes problem in Laravel and Page not found

I have contact form in pop-up but when I click on send button I've got page not found instead of redirect me to the home page. 我在弹出窗口中有联系表单但是当我点击发送按钮时我找不到页面而不是将我重定向到主页。

This is my route 这是我的路线

Route::post('/contact_us','HomeController@contact_us')->name('contact_us');

The function in HomeController.php 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');
}    

And the form opened and close tags are 表格打开和关闭标签

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

..... form inputs

{{Form::close()}}

When I click Submit I've got 当我点击提交时,我有

Not Found

The requested URL /contact_us was not found on this server.

Why is trying to load this URL when it should reload the home page? 为什么在重新加载主页时尝试加载此URL?

UPDATE with the form 更新表格

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

UPDATE 2: send mail function 更新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');
});

Please share the complete code of forms 请分享完整的forms代码

i have checked your function and form and routes there is no bugs 我检查了你的functionformroutes没有错误

I think that the data from the form is staring in the database but the redirecting after the store is throwing error 我认为表单中的数据正在database盯着,但是在存储之后redirecting会抛出错误

I am not daam sure but may be 我不是很确定,但可能是

So Try Changing the 所以尝试改变

FROM

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

TO

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

And also FROM 还有FROM

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

TO

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

Seems you are missing the csrf-token in the form. 似乎你错过了csrf-token Add the csrf field in the form as below: 在表单中添加csrf字段,如下所示:

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

OR 要么

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

Hope, this will resolve your issue. 希望,这将解决您的问题。

To answer your email problem 回答您的电子邮件问题

try to add the following in your .env file 尝试在.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

these credentials are for gmail-based email, don't forget to do these steps: 这些凭据适用于基于gmail的电子邮件,请不要忘记执行以下步骤:

  1. Go to your Google Account. 转到您的Google帐户。
  2. On the left navigation panel, click Security. 在左侧导航面板中,单击“安全性”。
  3. On the bottom of the page, in the Less secure app access panel, click Turn on access. 在页面底部的“不太安全的应用程序访问”面板中,单击“打开访问权限”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM