我的Laravel博客项目postcontroller的get方法正在运行,但post方法无效。我重定向了帖子方法。但Accordind我的代码应该返回我的管理页面。我的控制器代码是
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Category;
class PostController extends Controller
{
public function getBlogIndex() {
return view('frontend.blog.index');
}
public function getSinglePost($post_id,$end='frontend') {
return view($end . '.blog.single');
}
public function getCreatePost() {
return view('admin.blog.create_post');
}
public function postCreatePost(Request $request ) {
$this->validate($request, [
'title' => 'required|max:120|unique:posts',
'author' => 'required|max:80',
'body' => 'required'
]);
$post = new Post();
$post->title = $request['title'];
$post->author = $request['author'];
$post->body = $request['body'];
$post->save();
return redirect()->route('admin.index')->with(['success' => 'Post Successfully Created']);
}
}
我的路线文件
<?php
Route::group(['middleware' => ['web']], function () {
Route::group([
'prefix' =>'/admin'
], function() {
Route::get('/', [
'uses' => 'AdminController@getIndex',
'as' => 'admin.index'
]);
Route::get('/blog/posts/create', [
'uses' => 'PostController@getCreatePost',
'as' => 'admin.blog.create_post'
]);
Route::get('/blog/post/create', [
'uses' => 'PostController@postCreatePost',
'as' => 'admin.blog.post.create'
]);
});
});
我的表单是
@extends('layouts.admin-master')
@section('styles')
{!! Html::style('src/css/form.css') !!}
@endsection
@section('content')
<div class="container">
@include('includes.info-box')
<form action="{{ route('admin.blog.post.create') }}" method="post">
<div class="input-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" {{ $errors->has('title') ? 'class=has-error' : '' }} value="{{ Request::old('title') }}">
</div>
<div class="input-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" {{ $errors->has('author') ? 'class=has-error' : '' }} value="{{ Request::old('author') }}">
</div>
<div class="input-group">
<label for="category_select">Add Category</label>
<select name="category_select" id="category_select">
<option value="Dummy Category ID">Dummy Category</option>
</select>
<button type="button" class="btn">Add Category</button>
<div class="added-categories">
<ul></ul>
</div>
<input type="hidden" name="categories" id="categories">
</div>
<div class="input-group">
<label for="body">Body</label>
<textarea name="body" id="body" rows="12" {{ $errors->has('body') ? 'class=has-error' : '' }} >{{ Request::old('body') }}</textarea>
</div>
<button type="submit" class="btn">Create Post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
@endsection
@section('scripts')
{!! Html::script('src/js/posts.js') !!}
@endsection
我找不到问题所在。 Plz帮帮我
答案 0 :(得分:5)
这是一个非常常见的错误,也是一个值得注意的好错误。每当你看到:
RouteCollection.php中的MethodNotAllowedHttpException
首先要检查的是路线文件,以确保根据您的目的正确地Route::get
或Route::post
。
您的问题是您的表单将数据作为POST发送,但您的路由是GET。
<form action="{{ route('admin.blog.post.create') }}" method="post">
和
Route::get('/blog/post/create', [
'uses' => 'PostController@postCreatePost',
'as' => 'admin.blog.post.create'
]);
将其更改为Route::post
,以使其正常运行。