【问题标题】:MethodNotAllowedException when i click button to redirect to another view当我单击按钮重定向到另一个视图时出现 MethodNotAllowedException
【发布时间】:2017-08-14 05:27:37
【问题描述】:

我目前在 Laravel 中创建一个博客来学习 PHP/Laravel。问题是我得到了:

RouteCollection.php 第 218 行中的 MethodNotAllowedHttpException:

我有一个表格,其中显示所有帖子,每个帖子都有“查看”和“编辑”按钮。他们使用id 重定向到相关视图,并且工作正常。当我打开任何帖子查看时,还有一个编辑按钮,这是我点击它时出现错误的地方,我不知道为什么。

show.blade.php // 这是帖子的“视图”

<div class="row">
    <div class="col-md-8">
        <h1>{{ $post->title }}</h1>

        <p class="lead">{{ $post->body }}</p>
    </div>

    <div class="col-md-4">
        <div class="well">
            <dl class="dl-horizontal">
                <dt>Created at:</dt>
                <dd>{{ date('d M, y H:i', strtotime($post->created_at)) }}</dd>
            </dl>
            <dl class="dl-horizontal">
                <dt>Last Updated:</dt>
                <dd>{{ date('d M, y H:i', strtotime($post->updated_at)) }}</dd>
            </dl>
            <hr>
            <div class="row">
                <div class="col-sm-6">
                    <form method="POST" action="{{ route('posts.edit', $post->id) }}">
                        <input type="submit" value="Edit" class="btn btn-primary btn-block">
                        <input type="hidden" name="_token" value="{{ Session::token() }}">
                    </form>
                </div>
                <div class="col-sm-6">
                    <form method="POST" action="{{ route('posts.destroy', $post->id) }}">
                        <input type="submit" value="Delete" class="btn btn-danger btn-block">
                        <input type="hidden" name="_token" value="{{ Session::token() }}">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

edit.blade.php

<div class="row">
    <form method="POST" action="{{ route('posts.update', $post->id) }}">
        <div class="row">
        <div class="col-md-8">
            <div class="form-group">
                <label for="title">Title:</label>
                <textarea type="text" class="form-control input-lg" id="title" name="title" rows="1"
                          style="resize:none;">{{ $post->title }}</textarea>
            </div>
            <div class="form-group">
                <label for="body">Body:</label>
                <textarea type="text" class="form-control input-lg" id="body" name="body"
                          rows="5">{{ $post->body }}</textarea>
            </div>
        </div>
        <div class="col-md-4">
            <div class="well">
                <dl class="dl-horizontal">
                    <dt>Created at:</dt>
                    <dd>{{ date('M j, Y h:i:sa', strtotime($post->created_at)) }}</dd>
                </dl>

                <dl class="dl-horizontal">
                    <dt>Last updated:</dt>
                    <dd>{{ date('M j, Y h:i:sa', strtotime($post->updated_at)) }}</dd>
                </dl>
                <hr>
                <div class="row">
                    <div class="col-sm-6">
                        <a href="{{ route('posts.show', $post->id) }}" class="btn btn-danger btn-block">Back</a>
                    </div>
                    <div class="col-sm-6">
                        <button type="submit" class="btn btn-success btn-block">Save</button>
                        <input type="hidden" name="_token" value="{{ Session::token() }}">
        {{ method_field('PUT') }}
                    </div>
                </div>
            </div>
        </div>
        </div>
    </form>
</div>

PostController.php // 如果你也需要,这也是控制器

class PostController extends Controller {

public function index()
{
    //
    $posts = Post::all();

    return view('posts/index')->withPosts($posts);
}

public function create()
{
    // return create new post form
    return view("posts/create");
}

public function store(Request $request)
{
    // Validate data
    $this->validate($request, array(
        'title' => 'required|max:255',
        'body' => 'required'
    ));

    // store data in db
    $post = new Post;

    $post->title = $request->title;
    $post->body = $request->body;

    $post->save();

    Session::flash('success', 'The blog post was successfully saved!');

    // redirect if succesfull
    return redirect()->route('posts.show', $post->id);
}

public function show($id)
{
    //
    $post = Post::find($id);

    // redirect to view
    return view('posts/show')->with('post', $post);

}

public function edit($id)
{
    //
    $post = Post::find($id);

    return view('posts/edit')->with('post', $post);
}

public function update(Request $request, $id)
{
    //
}

public function destroy($id)
{
    //
}
}

【问题讨论】:

  • 你的路线呢?
  • 你能提供你的路线吗?可能您在路线中使用了错误的方法(获取而不是发布)。
  • Route::get('contact', 'PagesController@getContact'); Route::get('about', 'PagesController@getAbout'); Route::get('/', 'PagesController@getIndex'); Route::resource('posts', 'PostController');

标签: php laravel


【解决方案1】:

这是因为您使用的表单将方法定义为POST,但路由定义为GET

改成:

<form method="GET" action="{{ route('posts.edit', $post->id) }}">
    <input type="submit" value="Edit" class="btn btn-danger btn-block">
</form>

另外,您可以使用链接而不是表单:

<a href="{{ route('posts.edit', $post->id) }}" class="btn btn-danger btn-block">Edit</a>

【讨论】:

  • 是的..我太瞎了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2019-09-15
  • 1970-01-01
相关资源
最近更新 更多