有很多人遇到这个问题并且有一些解决方案,但我无法解决我的问题。编码就在那里。当我去localhost:8000 /文章我得到我的文章。没有问题。我可以去 / article / create 。但是当提交时,我在RouteCollection.php第219行错误中得到MethodNotAllowedHttpException。当我去 article / 2 时,我在RouteCollection.php第219行错误中收到MethodNotAllowedHttpException。
ArticleController.php
<?php
namespace App\Http\Controllers;
use App\Article;
//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
class ArticleController extends Controller
{
public function index(){
$articles = Article::all();
return view('articles.index',compact('articles'));
}
public function show($id){
$article = Article::findOrFail($id);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store(){
$input = Request::all();
return $input;
}
}
create.blade.php
@extends('app')
@section('content')
<h1>Write a New Article</h1>
<br/>
{!! Form::open(['url' => 'article']) !!}
<div class="form-group">
{!!Form::label('title','Title:')!!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body','Body:') !!}
{!! Form::textarea('body',null,['class' => 'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Article',['class'=> 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
@stop
index.blade.php
@extends('app')
@section('content')
<h1>Articles</h1>
<br/>
<?php foreach ($articles as $article): ?>
<article>
<h2>
<a href="/article/{{$article->id}}">{{$article->title}}</a>
</h2>
<div class="body">{{$article->body}}</div>
</article>
<?php endforeach ?>
@stop
routes.php文件
<?php
Route::get('about','PagesController@about');
Route::get('contact','PagesController@contact');
Route::get('article','ArticleController@index');
Route::get('article/create','ArticleController@create');
Route::get('article/{id}','ArticleController@show');
Route::post('article','ArticleController@store');
编辑1
php artisan route:list
输出:
GET|HEAD | about | App\Http\Controllers\PagesController@about
GET|HEAD | article | App\Http\Controllers\ArticleController@index
GET|HEAD | article/create | App\Http\Controllers\ArticleController@create
GET|HEAD | contact | App\Http\Controllers\PagesController@contact
编辑2和解决方案
php artisan route:clear
清除您的路线列表并执行php artisan route:list
您将获得所有路线。它对我来说很好。
答案 0 :(得分:1)
尝试在create.blade.php
中更改此内容:
{!! Form::open(['url' => 'article']) !!}
到此:
{!! Form::open(array('route' => 'article.store')) !!}
<强>更新强>
运行php artisan route:clear
以清除路由缓存。