2016-09-18 144 views
1

我tryng內置了通訊控制器,她是我的代碼:MethodNotAllowedHttpException在RouteCollection.php行218:

控制器

public function postNews(Request $request, $user) { 
    $this->validate($request, [ 'email' => 'required | email' ]); 

    $user = User::findOrFail($id); 
    $data = array(
     'email' => $request->email); 

     $token = $request->input('g-recaptcha-response'); 
     if (strlen($token) > 0) { 

     Mail::send('emails.newsletter', $data, function($message) use ($data) { 
     $message->from($data['email']); 
     $message->to($user->email, $user->name)->subject('A-Studio News Letter'); 
     //$message->subject($data['subject']); 
     }); 
     Session::flash('success', 'Grazie per esserti iscritto alla nostra news letter!'); 
     return view('blog.posts')->withPosts($posts); 
     }else { 
     return view('pages.nobot'); 
     } 
} 

路線

 Route::post('posts/{user}', ['uses' => '[email protected]', 'as' => 'blog.posts']); 

迴應 MethodNotAllowedHttpException在RouteCollection.php行218:

任何想法?

謝謝。

+0

仔細查看您的路線。是否有不同的路線具有'posts/{var}''?如果它在'POST'路由之前被定義,它會覆蓋它,並且你會得到一個'MethodNotAllowedHttpException',因爲該路由的格式不正確。 –

+0

請寄出調用'blog.post'路線的代碼。 –

+0

使用帖子路線,我想你正在提交一個表格。爲什麼使用動態URI「{user}」提交表單?在發送這篇文章時,你對你的路由中的那個'{user}'變量做了什麼? – Michel

回答

-1

你應該讓POST請求

<form action="posts/{{user}}" method="POST"> 
+0

嗨,我做到了,我得到這個mex:未定義變量:id,坦克的幫助! –

0
<form method="post" action="/posts"> 
<input name="email" type="email"> 
<button type="submit">subscribe</button> 
</form> 

Route::post('/posts', '[email protected]'); 

public function userSubscribe(Request $request){ 
    $data = $request->input('email'); 
    //Validate $data if necessary and save in the DB 

} 

這應該工作。

+0

這很有幫助,tankyou –

+0

很高興幫助。您可以將答案標記爲對可能有相同問題的其他人正確。 – Michel

0

您應該刪除,

Route::post('posts/{user}', '....');   

用作,

Route::post('posts', '....');   

,還可以使用與在參數$用戶變量控制器功能。

我希望這會幫助你。

+0

我做到了,沒有工作,但感謝您的幫助! –

相關問題