Laravel无法访问

时间:2017-06-22 14:16:39

标签: php laravel-5

在这里,我在Laravel中创建一个表单,通过路径“posts.store”将请求发布到控制器“PostsController”

        {!! Form::open(['url' => 'posts.store']) !!}
            {{Form::Label('title','Title:')}}
            {{Form::Text('title',null,array('class' => 'form-control'))}}
            {{Form::Label('body','Post Body:')}}
            {{Form::Textarea('body',null,array('class' => 'form-control'))}}
            {{Form::Submit('Create Post',array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top:20px;'))}}
        {!! Form::close() !!}

这是“PostsController”,我试图通过错误访问我的请求:

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

            ));
        //Store data into database
        $post = new Post;

        $post->title = $request->title;
        $post->body = $request->body;
        $post->save();

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

当我提交表单时,出现以下页面

NotFoundHttpException:

enter image description here

2 个答案:

答案 0 :(得分:0)

改变这个:

{!! Form::open(['url' => 'posts.store']) !!}

到此:

{!! Form::open(['url' => 'PostController@store']) !!}

要获得PostData,您需要:

$postData = $this->request->all();
$post->title = $postData['title'];
$post->body = $postData['body'];

答案 1 :(得分:0)

在你的NotFoundException屏幕截图中,你可以看到它将要访问的网址http://127.0.0.1:8000/posts.store显然这不起作用。将{!! Form::open(['url' => 'posts.store']) !!}更改为Form::open(['action' => 'PostsController@store', 'method' => 'POST'])。另外,请确保在web.php中设置了POST路由。