[英]laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218:
(SOLVED) Thanks.. (已解决)谢谢..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. 我只想创建一个名为“ tambah.blade.php”的新视图,控制器为方法“ tambahJurnal”的“ JurnalController.php”,但显示错误。 What's wrong with my route? 我的路线怎么了?
Here is my form: 这是我的表格:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController: 这是我在JurnalController中的方法:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route: 这是我的路线:
Route::post('/tambah', 'JurnalController@tambahJurnal');
And it show an error like this: enter image description here 并显示如下错误: 在此处输入图片描述
You are creating a route for the POST
method with this line: 您正在使用此行为POST
方法创建路由:
Route::post('/tambah', 'JurnalController@tambahJurnal');
But then, you're trying to perform a GET
request with your browser on that URL. 但是,然后,您尝试使用浏览器在该URL上执行GET
请求。 That's why you're getting that error. 这就是为什么您会收到该错误的原因。
Try adding this line as well: 也尝试添加此行:
Route::get('/tambah', 'JurnalController@tambahJurnal');
You can write this. 你可以写这个。 Hopefully this will solve your problem. 希望这可以解决您的问题。
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah" try with Route::any('/tambah', 'JurnalController@tambahJurnal'); 尝试使用action =“ / tambah”形式尝试Route :: any('/ tambah','JurnalController @ tambahJurnal'); first then if it works fine you can change to Route::post('/tambah', 'JurnalController@tambahJurnal'); 首先,如果工作正常,则可以更改为Route :: post('/ tambah','JurnalController @ tambahJurnal');
any will work for get post put .... 任何都会工作的...
Change this line 更改此行
Route::post('/tambah', 'JurnalController@tambahJurnal');
to 至
Route::post('tambah', 'JurnalController@tambahJurnal')->name('tambah');
and use blade Form
并使用刀片Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form
is , you don't explicitly need to specify {{ csrf_field() }}
, blade injects csrf token
itself. 使用Blade Form
优点是,您不需要显式指定{{ csrf_field() }}
,blade本身会注入csrf token
。
Add route to show view 添加路线以显示视图
Route::get('/tambah', 'JurnalController@index');
And add index method to your controller 并将索引方法添加到您的控制器
public function index(){
return view("tambah");
}
Also add /
同时添加/
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah
and trying to post url becomes /tambah/tambah
有时碰巧您在/tambah
,尝试发布url变为/tambah/tambah
Route::get('/tambah', function() { return view('tambah'); });
tambah.blade.php
file 更改tambah.blade.php
文件 <form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to 至
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.