Laravel form submit showing MethodNotAllowedHttpException in RouteCollection.php line 218: - php

This is my route
Route::resource('admin/reports', 'ReportController');
This is controller function
public function store(Request $request)
{
return "Thank you";
}
This is my html code
{!! Form::open([ 'url' => 'admin/reports/store', 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone', 'id' => 'reportfile' ]) !!}
{!! csrf_field() !!}
<div class="col-md-12">
<h3 style="text-align : center">Select File</h3>
</div>
<div class="col-md-12" style="text-align: center; padding: 10px">
<button type="submit" class="btn btn-primary">Upload Report</button>
</div>
{!! Form::close() !!}
When I submit the form, it show me MethodNotAllowedHttpException in RouteCollection.php line 218:
Any help is much appreciated. Thanks

Your form action should just be admin/reports.
Currently it will assume you are trying to post to the route admin/reports/{id}. That endpoint is used with GET, PUT and DELETE.
Check the docs including a table giving you routes https://laravel.com/docs/5.1/controllers#restful-resource-controllers if I were you I'd use the route helper to generate your urls for you

Related

Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD

I am trying to add an event on a calendar I have created, however I am getting the following error
The POST method is not supported for this route. Supported methods:
GET, HEAD
I have used the methods #csrf and {{ method_field('PUT') }} to no avail. I have also cleared route cache which did not help. Any help is much appreciated.
Routes:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function(){
Route::middleware('can:manage-users')->group(function(){
Route::resource('/users', 'UsersController', ['except' => ['show']]);
Route::resource('/courses', 'CoursesController', ['except' => ['show']]);
});
Route::middleware('can:manage-calendar')->group(function(){
Route::get('events', 'EventsController#index')->name('events.index');
Route::post('/addEvents', 'EventsController#addEvent')->name('events.add');
});
})
index.blade.php
#extends('layouts.app')
#section ('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-14">
<div class="card">
<div class="card-header">Calendar</div>
<div class="card-body">
{!! Form::open(array('route' => 'admin.events.index', 'method' => 'POST', 'files' => 'true'))!!}
{{-- {{method_field('PUT') }}
#csrf --}}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12"></div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
{!! Form::label('event_name', 'Event Name:') !!}
<div class="">
{!! Form::text('event_name', null, ['class' => 'form-control']) !!}
{!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!}
</div>
#Collin, I have added the image below in relation to your question
The error actually explains the problem. The method POST is not supported for the route you're using. You are trying to post to the route: admin.events.index but you actually want to post to the route events.add.
Route::post('/addEvents', 'EventsController#addEvent')->name('events.add');
{!! Form::open(array('route' => 'admin.events.add', 'method' => 'POST', 'files' => 'true'))!!}
{{-- #csrf --}}
Adding to this awnser is a possible solution for the validator exception the OP has mentioned in the comments.
The validator not found error can possibly come from the following:
When adding the the following code:
public function addEvent(Request $request)
{
$validator = Validator::make($request->all(),
[ 'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required' ]);
if ($validator->fails())
{ \Session::flash('warning', 'Please enter the valid details'); return Redirect::to('admin.events.index')->withInput()->withErrors($validator);
Try adding:
use Illuminate\Support\Facades\Validator;
Just check your form action url route. You have to pass 'route('admin.events.add)' rather than 'route('admin.events.index')' and also dont use 'PUT' it will accept 'POST' as well.

Controller method not defined in laravel

I am new in Laravel and trying to learn forms. Currently I am trying to do a file uploading form and my create page looks like this:
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
{!! Form::open(['action' => 'MovieController#create', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('name', 'Name')}}
<br>
{{Form::text('name')}}
</div>
<div class="form-group">
{{Form::label('description', 'Description')}}
<br>
{{Form::textarea('description')}}
</div>
<div class="form-group">
{{Form::label('release_date', 'Release Date')}}
<br>
{Form::date('release_date')}}
</div>
<div class="form-group">
{{Form::label('country', 'Country')}}
<br>
{{Form::text('country')}}
</div>
<div class="form-group">
{{Form::label('poster_name', 'Poster Image')}}
{{Form::file('poster_name')}}
</div>
<div class="form-group">
{{Form::label('file_name', 'Movie File')}}
{{Form::file('file_name')}}
</div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
</body>
</html>
As you can see I am trying to make an action of 'MovieController#create'. Now let's see MovieController file:
namespace
App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movie;
class MovieController extends Controller
{
public function create(Request $request){
$this->validate($request,[
'poster_name' => 'required|image'
]);
//Handle poster upload
$imageName = $request->file('poster_name')->getClientOriginalName();
$request->file('poster_name')->storeAs('public/images',$imageName);
$videoName = $request->file('file_name')->getClientOriginalName();
$request->file('file_name')->storeAs('public/videos',$videoName);
$movie = new Movie;
$movie->name = $request->name;
$movie->description = $request->description;
$movie->release_date = $request->release_date;
$movie->country = $request->country;
$movie->poster_name = $imageName;
$movie->file_name = $videoName;
$movie->save();
$movies = Movie::all();
return view('home',['movies' => $movies]);
}
}
At the beginning everything was working but then I made a few changes in create file (only css and visual changes) and now when I try to go to that page, it gives the following error:
ErrorException
Action App\Http\Controllers\MovieController#create not defined. (View: C:\xampp\htdocs\MagicMovie\resources\views\movies\create.blade.php)
Any suggestions?
Try doing something like this,
On you form
{!! Form::open([ 'action' => route('create_movie'),
'enctype' => 'multipart/form-data', 'method' => 'POST']) !!}
or
{!! Form::open([ 'action' => url('movie/create'),
'enctype' => 'multipart/form-data' , 'method' => 'POST' ]) !!}
And on your routes (web.php)
Route::post('movie/create', ['uses' => 'MovieController#create', 'as' => 'create_movie']);
You can also check some basic Laravel routing here.
I think you can try this :
Route::post('/storeMovie', 'MovieController#create')->name('storeMovie');
in form action
{!! Form::open(['route' => 'storeMovie', 'enctype' => 'multipart/form-data']) !!}
Hope this work for you !!!
You are binding controller method to form directly, you can't do that.
You should write a Route for it.
in web.php
Route::post('/add', 'MovieController#create')->name('create_movie');
in form action
'action' => 'create_movie'

Route not defined error in laravel

i'm using form collective in laravel for 2 difference form and got error. Here is my view
<div class="table-list-donor">
{!! Form::model($transactions, ['route' => ['admin.update',$transactions->id ],'file'=>true, 'id'=>'project-form','method'=>'POST']) !!}
#include('admin.patials.form',['title' => 'edit form','submit' => 'edit'])
{!! Form::close() !!}
</div>
and here is my route
<td class="action">
edit
</td>
error is Route [] not defined in route . please help!

Laravel 5 function redirect to third party url

How do I pass all form data function to third party URL. Suppose my third URL is
`http:\\www.abletoaccess.com\form\request`
For the security reason I don't want to access this URL in form action method or I don't want to post direct form data. I want when I submit the form all data comes in my function and redirect to third party URL with post data and added more parameters.
Any help will be appreciated!!!!
You can handle the request by your controller and then show view and auto-submit it to external url.
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
Page is loading...
</div>
<div class="panel-body">
{!! Form::open(['url' => $externalUrl, 'method' => 'POST', 'class' => 'form-horizontal', 'id' => 'my-form']) !!}
#foreach ($fields as $key => $value)
{{ Form::hidden($key, $value) }}
#endforeach
{{ Form::hidden('signature', $signature) }}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('my-form').submit();
</script>
Try Following Code:
<form method="post" action={{ action('Controller#method') }}>
<input type="submit" value="add">
</form>
in controller file write below code :
public function method(Request $request)
{
Redirect::away('external url')->withInputs(Input::all());
}
Laravel 5: how to redirect with data to external resource form controller
First of all, this seems not to be Laravel specific issue.
Second, you need to check CURL function in PHP.
PHP CURL
If you can, install Guzzle client that helps with these things.
You can pass the client to your controller and do a post request to another URL like this:
public function someMethod(GuzzleHttp\Client $client)
{
$client->post('http:\\www.abletoaccess.com\form\request', [
'form_params' => [
'param1' => 'something',
...
]
]);
}

laravel 5.1 auth csrf token mismatch

before make any judgment I read all the related questions related to my problem but none of them fixed it.
so here's my problem when I use the authentication facility of laravel 5.1 and want to register a user the csrf token generate twice one when I requesting to show my register form and one when I post the form data to auth/register post route and this cause my to receive a csrf token mismatch exception. here's my register form markup
<form method="POST" action="/auth/register" class="ui large form">
{!! csrf_field() !!}
<div class="two fields dirright alignright">
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'first_name',
Input::old('first_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => 'نام'
)
) !!}
</div>
</div>
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'last_name',
Input::old('last_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => 'نام خانوادگی'
)
) !!}
</div>
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="mail icon"></i>
{!! Form::email(
'email',
Input::old('email'),
array(
'class' => 'latintext',
'placeholder' => 'E-mail address'
)
) !!}
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="lock icon"></i>
{!! Form::password(
'password',
Input::old('password'),
array(
'class' => 'latintext',
'placeholder' => 'Password'
)
) !!}
</div>
</div>
<div class="ui fluid large primary submit button">ثبت نام</div>
<div class="ui error message alignright"></div>
</form>
Just add the csrf token as follows in the form :
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
it worked for me.
Assume that your web server has already write access to session directory, in my case 'app/storage/framework/sessions/'.
Execute:
$ rm -f {your_web_app}/storage/framework/sessions/*
There are several possibilities...
1) If you have any spaces at all in front of your opening <?php tag, it can cause this error (especially if you're using AJAX). So just double-check to make sure that there's nothing before <?php in your files.
2) If you're trying to submit this form data via AJAX, the docs suggest passing the CSRF token like so:
Add this meta tag to your <head>:
<meta name="csrf-token" content="{{ csrf_token() }}">
And then do this in the AJAX call:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
If your using laravel 5.1 simply adding {{ csrf_field() }} would do the trick
The csrf token will be added automatically if you use the open and close tags for Form
{!! Form::open(['action' => '/auth/register', 'class' => 'ui large form']) !!}
-- Form stuff here --
{!! Form::close() !!}
i hope this will help
set meta-tag like follows
<meta name="csrf-token" content="{{ csrf_token() }}">
then request like follows
$.ajax({
data: {data1:'data1',data2:'data2'},
url: '/your/url/goes/here',
type: 'POST',
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
},
success: function(response){
console.log(response);
}
})

Categories