单页网站上的Laravel联系表格

时间:2018-02-01 21:00:27

标签: php laravel-5.5 contact-form

我试图在单页网站上获取Laravel联系表单的正确路线,但我不确定如何应用这些路线到目前为止,因为我已经完成了网站那不是单页(视差 - 像网站/可滚动)。

这些是我正在创建的路线,因此所有内容都保留在主页上(完全没有重定向,因为它是一个可滚动浏览的网站)

 Route::get('/', 'ContactUsController@create')->name('contact.create');
 Route::post('/', 'ContactUsController@store')->name('contact.store');

我的控制器看起来像这样:请注意,创建控制器将视图返回到我的索引,当然这样的路由就是这样(' /'),

    namespace App\Http\Controllers;

        use Illuminate\Http\Request;
        use App\Http\Requests;
        use App\Mail\ContactEmail;

        class ContactUsController extends Controller
        {
            public function create()
            {

                return view('index');
            }

            public function store(Request $request)
            {
                $contact = [];

                $contact['name'] = $request -> get('name');

                $contact['phone'] = $request -> get('phone');
                $contact['email'] = $request -> get('email');

                $contact['subject'] = $request -> get('subject');
                $contact['message'] = $request -> get('message');

                //send mail logic here

Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));

                flash('Your Message has been sent!) -> success();
                return redirect()-> route('/');
            }
        }

以下是我的联系表格:

{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}

  {!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
  {!! Form::text('name', null, ['class' => 'form-control text-light'])!!}


  {!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
 {!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}


 {!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
 {!! Form::text('email', null, ['class' => 'form-control text-light'])!!}

 {!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
 {!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}

 {!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
 {!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}

 {!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}

      {!! Form::close() !!}

              @if($errors -> any())
                 <div class="alert alert-danger">
                        <ul>
                     @foreach($errors -> all() as $error)
                             <li>{{ $error }}</li>
                                  @endforeach
                        </ul>
                  </div>
              @endif
              @include('flash::message')

解决了解决方案的问题:

//routes for contact form
Route::get('/contacts', 'ContactUsController@create')->name('contact.create');
Route::post('/contacts', 'ContactUsController@store')->name('contact.store');

其他一切都一样。 谢谢大家的支持。

2 个答案:

答案 0 :(得分:2)

//在web.php中创建路由

Route::get('contact',['as' => 'contact, 'uses' => 'ContactController@create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController@store']);

答案 1 :(得分:1)

//使用命令创建ContactController  php artisan make:controller ContactController

<?php
   namespace App\Http\Controllers;

   use Illuminate\Http\Request;
   use App\Http\Requests\ContactRequest;
  use Mail;

   class ContactController extends Controller
  {
   public function create()
   {
     return view('front.contact.index');
   }

   public function store(ContactRequest $request)
    {

     \Mail::send('emails.contact',
    array(
        'name' => $request->get('name'),
        'email' => $request->get('email'),
        'user_message' => $request->get('message')
    ), function($message)
{
    $message->from('hello@outlined.co');
    $message->to('poornima@outlined.co', 'outlined')->subject('New Contact 
         Request from outlined');
});
return back()->with('status', 'your message has been received');
}}