1
votes

I'm tryng to built a controller for a newsletter, her is my code:

Controller

    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

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

Response MethodNotAllowedHttpException in RouteCollection.php line 218:

Any idea?

Thank you.

3
Take a close look at your routes. Is there a different route that has 'posts/{var}'? If it's defined before your POST route it'll overwrite it and you get a MethodNotAllowedHttpException because that route is not formatted correctly.Roemer
Please post code which calls blog.post route.Alexey Mezenin
Using a post route, I guess you are submitting a form. Why do you submit a form using a dynamic URI {user}? When sending this post what do you do to that {user} variable in your route?Michel
@Michel Yep i'm submitting a form, it has only one input (email) actually it is a subscribe form one input one button. I'm using a dyn uri because a Whoops pop up asking for 2 Missing argument..Alessio Wildgiorg
@RoemerBakker here is the blog route list: Route::get('posts', 'BlogController@getPosts'); ; Route::post('posts/{user}', ['uses' => 'BlogController@postNews', 'as' => 'blog.posts']); Route::get('blog/{title}', ['uses' => 'BlogController@getArticoli', 'as' => 'blog.articoli']);Alessio Wildgiorg

3 Answers

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

Route::post('/posts', 'YourController@userSubscribe');

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

}

This should work.

0
votes

You should remove the,

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

use as,

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

And also use the controller function with $user variable in the parameters.

I hope this will help you.

-1
votes

You should make POST request

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