如何显示表单中的信息?

时间:2014-09-22 15:05:35

标签: php symfony laravel repository

我正在尝试显示从表单提交的信息以创建留言板。我在laravel框架中使用php。我正在使用表单,记录和存储库。每当我尝试显示内容时,我收到以下错误Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException。

这是控制器:

 /**
 * Display a listing of the resource.
 * GET /messages
 *
 * @return Response
 */
public function create()
{
    return View::make('comments.create');
}



 public function show($comment)
{
    $message_id = $this->messageRepository->find($comment);
    return View::make('comments.show')->with('comment', $message_id);
}

/**
 * Store a newly created resource in storage.
 * POST /messaages
 *
 * @return Response
 */
public function store()
{
    $data = Input::all() ;
    $this->messageForm->validate($data);

    $messageRecord = new MessageRecord;
    $messageRecord->comment = $data['comment'];

    Return "Comment created";
}
}

以下是引起麻烦的观点:

<p>$comment</p>

我没有使用过一条路线,但我把它扔在了一起:

Route::resource('/message', 'MessageController');

2 个答案:

答案 0 :(得分:1)

<强> [编辑:]

如果您的store()已被其他表单使用,但您还有另一个表单要发送POST数据,那么您应该为此声明另一个路由。像

Route::post('message/display', ['as' => 'message.display', 'uses' => 'MessageController@display');

很抱歉我以前的回答,show()用于GET请求,因为您使用的是表单,除非您决定通过网址发送数据,否则您可能不需要它。我非常抱歉。

[编辑结束]

您收到该错误是因为您不允许在资源丰富的路由中使用display()。相反,您必须使用store()。由于您使用的是laravel资源路由,因此您应该遵循laravel的约定。在命令行中,运行

php artisan routes 

你会得到这样的东西,

    GET|HEAD message                | message.index   | MessageController@index
    GET|HEAD message/create         | message.create  | MessageController@create
    POST message                    | message.store   | MessageController@store
    GET|HEAD message/{message}      | message.show    | MessageController@show
    GET|HEAD message/{message}/edit | message.edit    | MessageController@edit
    PUT message/{message}           | message.update  | MessageController@update
    PATCH message/{message}         |                 | MessageController@update
    DELETE message/{message}        | message.destroy | MessageController@destroy

这些是您应该使用的方法,路由名称及其相应的HTTP请求。

Check Laravel documentation for resourceful controller

快速查看,

index : Display a listing of the resource,
create : Show the form for creating a new resource, 
store : Store a newly created resource in storage,
show : Display the specified resource,
edit : Show the form for editing the specified resource,
update : Update the specified resource in storage,
destroy : Remove the specified resource from storage

答案 1 :(得分:0)

对于显示此路线的路线,您似乎使用了不正确的动词。对于display,您应该使用Route::get,并且可能使用Route::post。如果不是问题,请编辑您的问题并按照评论中的要求放置routes.php

相关问题