forms - Lavavel 5.2.36 MethodNotAllowedHttpException in RouteCollection.php line 218: -
hi new laravel , trying implement post request simple form. have been following youtube tutorial series (laravel 5 | section 3 | part 4 routing post requests) @ 5:46mins in, there notification method applicable versions prior laravel 5.2.
i have tried edit verifycsrftoken.php method protected $except = ['api/']; makes no difference.
my routes.php code snippet:
route::post('/form-handler', function(\illuminate\http\request $request){ if(isset($request['option']) && $request['firstname']) { if(strlen($request['firstname']) > 0){ return view('forms.formresults', ['action' => $request['option'], 'name' => $request['firstname']]); } return redirect()->back(); } return redirect()->back(); // return user page came })->name('form-handler');
my welcome.blade.php code snippet:
<div class="form-group"> <form action="{{route('form-handler')}}" method="post" class="form-control"> <label for="select-action">select option:</label> <select id="select-action" name="option"> <option value="option1">option1</option> <option value="option2">option2</option> <option value="option3">option3</option> </select> <input type="text" name="firstname"> <button type="submit">submit details</button> <input type="hidden" value="{{session::token()}}" name="_token"> <!-- laravel security measure, note name must '_token'--> </form> </div>
my full error message:
methodnotallowedhttpexception in routecollection.php line 218: in routecollection.php line 218 @ routecollection->methodnotallowed(array('post')) in routecollection.php line 205 @ routecollection->getrouteformethods(object(request), array('post')) in routecollection.php line 158 @ routecollection->match(object(request)) in router.php line 821 @ router->findroute(object(request)) in router.php line 691 @ router->dispatchtoroute(object(request)) in router.php line 675 @ router->dispatch(object(request)) in kernel.php line 246 @ kernel->illuminate\foundation\http\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 52 @ pipeline->illuminate\routing\{closure}(object(request)) in debugbar.php line 49 @ debugbar->handle(object(request), object(closure)) @ call_user_func_array(array(object(debugbar), 'handle'), array(object(request), object(closure))) in pipeline.php line 136 @ pipeline->illuminate\pipeline\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 32 @ pipeline->illuminate\routing\{closure}(object(request)) in checkformaintenancemode.php line 44 @ checkformaintenancemode->handle(object(request), object(closure)) @ call_user_func_array(array(object(checkformaintenancemode), 'handle'), array(object(request), object(closure))) in pipeline.php line 136 @ pipeline->illuminate\pipeline\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 32 @ pipeline->illuminate\routing\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 102 @ pipeline->then(object(closure)) in kernel.php line 132 @ kernel->sendrequestthroughrouter(object(request)) in kernel.php line 99 @ kernel->handle(object(request)) in index.php line 53
is method of implementation invalid or need few tweeks?
preferably know optimal post method retrieving form data, while still protecting site cross-site request forgeries.
any fixes or alternate solutions appreciated.
please feel free ask resources may have left out.
full routes.php (note have tried adding post method middleware route group):
<?php /* |-------------------------------------------------------------------------- | application routes |-------------------------------------------------------------------------- | | here can register of routes application. | it's breeze. tell laravel uris should respond | , give controller call when uri requested. | */ /* route::get('/', function () { return view('home.welcome'); }); */ route::get('/', 'welcomecontroller@index')->name('home'); route::resource('lists', 'listscontroller'); route::get('/contact/{firstname?}', function($firstname = null) { return view('about.contact', ['firstname' => $firstname]); })->name('contact'); route::get('/blog', function() { return view('blog.blog'); })->name('blog'); route::group(['middleware' => 'auth'], function () { route::post('/form-handler', function(\illuminate\http\request $request){ if(isset($request['option']) && $request['firstname']) { if(strlen($request['firstname']) > 0){ return view('forms.formresults', ['action' => $request['option'], 'name' => $request['firstname']]); // create view } return redirect()->back(); } return redirect()->back(); // return user page came })->name('form-handler'); });
edited answer --
i have edited answer make cleaner code has need. instead of using isset, uses validation not stop running code if options not set. display error message on blade view.
your routes.php file should contain following
route::post('/form-handler', 'formcontroller@formhandle');
the blade view must have form looks this
@if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="form-group"> <form action="{{route('form-handler')}}" method="post" class="form-control"> <label for="select-action">select option:</label> <select id="select-action" name="option"> <option value="option1">option1</option> <option value="option2">option2</option> <option value="option3">option3</option> </select> <input type="text" name="firstname"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <button type="submit">submit details</button> <input type="hidden" value="{{session::token()}}" name="_token"> </form>
the form controller must
<?php namespace app\http\controllers; use illuminate\http\request; use illuminate\routing\controller; class formcontroller extends controller { /** * logic goes here. * * @param request $request * @return response */ public function formhandle(request $request) { $this->validate($request, [ 'firstname' => 'required|min:1|max:255', 'option' => 'required' ]); $data = $request; return view('forms.formresults')->compact($data); } }
Comments
Post a Comment