【问题标题】:405 Error when trying to send email via route with Laravel尝试使用 Laravel 通过路由发送电子邮件时出现 405 错误
【发布时间】:2015-04-05 22:17:15
【问题描述】:

尝试使用 Laravel 发送电子邮件,但不断收到 405 错误并显示糟糕的页面:

 * @param  array  $others
 * @return void
 *
 * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
 */
protected function methodNotAllowed(array $others)
{
    throw new MethodNotAllowedHttpException($others);
}

代码是:

{{ Form::open(array('url' => 'admin/newemail')) }}
  // form items
{{ Form::close() }}

路线是:

Route::get('admin/newemail', function()
{
    $email = 'email@hotmail.com';
    $data = array('s' => Input::get('email-heading'));
    Mail::send('emails.wereback', $data, function($message) use ($email){
        // $message details
    });
});

但是,如果我直接转到 url admin/newemail 它工作正常。

有什么帮助吗?

【问题讨论】:

    标签: php email laravel laravel-4 laravel-routing


    【解决方案1】:

    默认情况下,Form 助手将生成一个使用 POST 方法的 html 表单。如果您需要使用不同的方法,您可以指定方法:

    {{ Form::open(array('url' => 'admin/newemail', 'method' => 'GET')) }}
    

    或者您也可以更改路由以匹配 POST 请求:

    Route::post('admin/newemail', function()
    {
        // [...]
    });
    

    【讨论】:

    • 我会选择后者,这样人们就不会意外访问example.com/admin/newemail 并发送电子邮件。
    • 啊,是的!我脑子里的逻辑并没有正确地考虑这个问题。帖子是我不需要得到的...... :)
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多