【问题标题】:Contact Form Laravel 4- Troubles with routes联系表格 Laravel 4- 路线问题
【发布时间】:2016-07-21 08:30:43
【问题描述】:

我正在尝试填写此联系表,但我是:(

我有这个表格:

{{ Form::open(array('url' => 'PagesController@contact', 'method' => 'POST',  'role' =>'form', 'class' =>'form-horizontal' )) }}
    <div class="form-group">
        {{ Form::label('name', 'Nombre:', ['class' => '']) }}                       
        {{ Form::text('name', '', ['class' => 'form-control', 'maxlength' => 20]) }}
    </div>

    <div class="form-group">
        {{ Form::label('email', 'Email:', ['class' => '']) }}                   
        {{ Form::email('email', '', ['class' => 'form-control']) }}
    </div>

    <div class="form-group">
        {{ Form::label('subject', 'Asunto:', ['class' => '']) }}                    
        {{ Form::text('subject', '', ['class' => 'form-control']) }}
    </div>
        <div class="form-group">    
        {{ Form::label('country', 'Pais:', ['class' => '']) }}                  
        {{ Form::text('country', '', ['class' => 'form-control']) }}
    </div>

    <div class="form-group">
        {{ Form::label('textarea', 'Message:', ['class' => '']) }}                          
        {{ Form::textarea('msg', '',  ['class' => 'form-control', 'placeholder' =>'Your Message', 'required'=>'true'])  }}
    </div>


    <div class="form-group">
        {{ Form::submit('Send', array('class' => 'btn btn-primary btn-md')) }}
    </div>


{{ Form::close() }}

在视图/emails/contact.blade.php

Name:   {{ $name }}
Email:  {{ $email }}
Country:    {{ $country }}
Subject:    {{ $subject }}
Message: {{ $msg }

在我的 App/Config/mail.php 中:

return array(   
   'driver' = 'smtp',  
   'host' = 'smtp.gmail.com',  
   'port' = 587,  <br/> 
   'from' = array('address' = 'africamia@gmail.com', 'name' = 'Admin'),  
   'encryption' = 'tls',   
   'username' = 'africamia@gmail.com',   
   'password' = '123456',   
   'sendmail' =  '/usr/sbin/sendmail -bs',   
   'pretend' = false,  
); 

在我的 Pages@Controller.php 中

public function contact()
{
    $validation = New ?? I don't know what I do here :(;

    if($validation->passes()) {

       $fromEmail = Input::get('email');
       $fromName = Input::get('name');
       $subject = "Email from someone at website.com";
       $data = [ 'msg' => Input::get('message') ];

       $toEmail = 'africamia83@gmail.com';
       $toName = 'Admin';

       Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){

           $message->to($toEmail, $toName);

           $message->from($fromEmail, $fromName);

           $message->subject($subject);
       });

    return Redirect::to('/gracias')
        ->with('message', 'Your message was successfully sent!');
    }

    return Redirect::back()
        ->withInput()
        ->withErrors($validation->errors);
}

在路线中

Route::get('contact', array('as' =>'contact', 'uses'=> 'PagesController@contact'));

Route::get('contact', function() {

return View::make('contact');

});

我有这个错误:

当我点击n提交按钮时,在浏览器中出现:../public/PagesController@contact

异常处理程序中的错误:没有为 PagesController@contact 定义路由 [/]

【问题讨论】:

  • 尝试用'action' =&gt; 'PagesController@contact'替换'url' =&gt; 'PagesController@contact'
  • 您在 routes.php 中定义了两次Route::get('contact')...。您需要定义发布表单时会发生什么:Route::post('contact')...

标签: php laravel-4


【解决方案1】:

当您提交 for 时,它是 POST 请求,而不是 GET。您将其定义为 GET 请求。请更改路由文件如下:

Route::post('contact', 'PagesController@contact');

【讨论】:

  • 非常感谢您的支持。我使用 Laravel Doc,这很难。我创建了一个:function contact(){$validation = new Services\Validators\Contact;我在 App/Validator/contact.php 中添加了一个文件夹:&lt;?php namespace pp\Extension\Validation;use Illuminate\Validation\Validator;class CustomValidation extends Validator{public function alpha_spaces($attribute, $value, $parameters) {if(preg_match("/^([-a-z0-9_ ])+$/i", $value)){return true; } return false;}}` 我有:找不到类“Services\Validators\Contact”
  • 最好您制作请求文件以进行验证。为此,请打开您的终端并输入:php artisan make:request Contact
猜你喜欢
  • 2013-12-22
  • 1970-01-01
  • 2013-08-15
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
相关资源
最近更新 更多