我的网页标题中有回拨按钮,因此用户可以从每个页面向我发送消息。
在routes.php中有特殊的Route::post
我决定创建PostMiddleware
这样做我希望在我的网站的每个页面上都会分析所有POST请求,如果它们包含字段formname
,那么JSON数据就会通过邮件发送给我。
但我有几个问题:
1)回调表单从ALMOST我的网站的每一页都运行良好,但无论如何我每次都可以在控制台中看到405错误代码。据我了解这个错误,因为我没有特殊的POST请求路由。但我不知道如何删除此错误。只是添加Route::post
不是我想要做的,因为我希望中间件应该更好。
2)回调表单无法在网页上运行,我有另一种表单<form ng-app="SaunaDoorCalc" ng-controller="SaunaDoorCalcController" class="calculator" name="calculator">
。在这个网页上只有SaunaDoorCalc表格正常工作。当我点击ng-click="submit(callback.$valid)"
时,控制台中没有任何反应。
按钮
<a class="btn btn-primary btn-large" href="#callback" title="Обратный звонок" data-toggle="modal" data-target="#callback">
然后这是我的回调表格的HTML
<div class="modal fade" id="callback" tabindex="-1" role="dialog" aria-labelledby="callback" aria-hidden="true">
<div class="modal-dialog">
<form ng-app="callback" name="callback" ng-controller="callbackController" role="form" class="" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="CallbackModalLabel"><i class="fa fa-envelope-o text-primary"></i> Отправить письмо / Попросить перезвонить</h4>
</div>
<div class="modal-body">
<div class="text-primary">* обязательное поле</div>
<div class="col-sm-6">
<div class="input">
<input id="name" name="name" ng-model="name" type="text" required>
<label for="name">Ваше имя *</label>
</div>
<div class="input">
<input id="phone" name="tel" ng-model="tel" type="text" required>
<label for="phone">Номер телефона *</label>
</div>
<div class="input">
<input id="time" name="time" ng-model="time" type="text">
<label for="time">Желательное время звонка</label>
</div>
<div class="input">
<input id="email" name="email" ng-model="email" type="email">
<label for="email">Адрес электронной почты</label>
</div>
</div>
<div class="col-sm-6">
<div class="input">
<textarea id="msg" name="msg" ng-model="msg"></textarea>
<label for="message">Сообщение *</label>
</div>
</div>
<div class="clearfix"></div>
<div class="input-group margin-bottom-20">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
<i class="fa fa-paperclip"> </i>
Прикрепить файл
<input type="file" name="file" id='file' multiple="">
</span>
</span>
<input type="text" class="form-control" readonly="">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Закрыть</button>
<button type="submit" class="btn btn-primary pull-right" data-dismiss="modal" ng-click="submit(callback.$valid)">Отправить</button>
</div>
</div>
</form>
</div>
</div>
这里是角度模块
angular
.module('callback', [])
.controller('callbackController', function($scope, $http) {
$scope.outerScope = {};
$scope.formData = {};
$scope.url = '/';
$scope.submit = function(isValid) {
if (isValid) {
$http.post($scope.url,
{
"formname": "callback", // this field to distinguish different ajax forms, which have different mail template
"name": $scope.name,
"tel": $scope.tel,
"time": $scope.time,
"email": $scope.email,
"msg": $scope.msg,
}).
success(function(data, status) {
console.log(data);
$scope.status = status;
$scope.data = data;
$scope.result = data;
});
$scope.name = null;
$scope.tel = null;
$scope.email = null;
$scope.msg = null;
$scope.callback.$setPristine();
}else{
$('.errors').modal('show');;
}
}
});
app \ Http \ Middleware \ PostMiddleware.php
namespace App\Http\Middleware;
use Closure;
use URL;
use Event;
use App\Events\SendMail;
class PostMiddleware
{
public function handle($request, Closure $next)
{
if ($request->formname) {
$result = $request->all();
Event::fire(new SendMail($result));
}
return $next($request);
}
}
\应用\活动\ SendMail.php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SendMail extends Event
{
use SerializesModels;
public $result;
public function __construct($result)
{
$this->result = $result;
}
public function broadcastOn()
{
return [];
}
}
\应用\监听\ SendMailFired.php
namespace App\Listeners;
use Mail;
use App\Events\SendMail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendMailFired
{
public function __construct()
{
//
}
public function handle(SendMail $event)
{
$mailarray = $event->result;
switch ($mailarray['formname']) {
case "callback":
Mail::send('emails.callback', $mailarray, function($message) use ($mailarray) {
$message->from($mailarray['email'], $mailarray['name'] );
$message->to('info@site.ru', 'noreply');
$message->setCc($mailarray['email'], $mailarray['name']);
$message->replyTo($mailarray['email'], $mailarray['name'] );
$message->subject('Обратный звонок с сайта');
});
break;
case "saunadoor":
Mail::send('emails.sauna-calculator', $mailarray, function($message) use ($mailarray) {
$message->from($mailarray['email'], $mailarray['name'] );
$message->to('info@site.ru', 'noreply');
$message->setCc($mailarray['email'], $mailarray['name']);
$message->replyTo($mailarray['email'], $mailarray['name'] );
$message->subject('Заказ двери для сауны');
});
break;
}
}
}
\应用\ HTTP \ Kernel.php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\PostMiddleware::class,
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];
}
这是路线错误
1/1 MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 750
at Router->findRoute(object(Request)) in Router.php line 659
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in PostMiddleware.php line 26
at PostMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(PostMiddleware), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54