Post-Parameters as Action Input
I get the following error message after submitting a form:
Controller "Foo\ModelBundle\Controller\PostController::createAction()" requires that you provide a value for the "$title" argument (because there is no default value or because there is a non optional argument after this one).
The form is not created by the symfony framework form feature, its just plain html in the twig template:
<form action="{{ path('post') }}" method="post">
Title: <input name="title"><br>
Author: <input name="author"><br>
Content: <input name="content"><br>
<input type="submit">
</form>
This is the routing information for the post route:
post:
pattern: 开发者_C百科 /post
defaults: { _controller: FooModelBundle:Post:create }
requirements:
_method: POST
This is the defintion of the create action:
public function createAction($title, $author, $content) {
The symfony profiler for the request shows me, that $title, $author and $content is set to the correct values.
What is the correct way to provide the action with the needed parameters?
Shouldn't they populated by the framework with the values in the POST request?
I'm using symfony 2.0 PR 10.
Crossposted this also here: http://forum.symfony-project.org/viewtopic.php?f=23&t=34427
I've tried to answer this for you where you originally posted the question at http://forum.symfony-project.org/viewtopic.php?f=23&t=34427&p=115961#p115961
The ideas is to use the form generation features of the framework and retrive the data via the request object.
I hope you'll find it very helpful.
The reason for not using the frameworks form generation and validation features are the following:
What I wanted to achieve is a quick test of an action with some dynamic data. At this point I don't want to write x lines of code for the data binding and form creation. Its the next step.
The thing I got wrong was the concept of the parameters in the routing.
My thought was if I use /{foo}/{bar} as a matcher, its a simple GET request. The parameters will get populated to the controller without any further configuration. If I now use a POST request, the parameters should get also populated to the action.
The point is, that {foo} and {bar} are parts of the URI and not any GET parameters, like ?val=xy. GET/POST parameters have nothing to do with the input parameters for the action. They are accessed through the request object and of course not passed to the action as method parameters.
for this function : public function createAction($title, $author, $content)
routing must be like this :
post:
pattern: /post/{title}/{$author}/{content}
defaults: { _controller: FooModelBundle:Post:create }
requirements:
_method: POST
精彩评论