Laravel 4 Form::open set action

2019-05-23 06:31发布

问题:

I'm currently trying out Laravel 4 and I have created a resource controller. In the 'edit' function I'm building a form, which should post to the 'update' function.

To create the form open tag I use the Form::open() function which recently got added to Laravel 4 it seems. But when I just do Form::open() the action of the form is the current url and I can't figure out how to change the action.

I tried Form::open('clients/' . $client->id) but this gives me the following error:

ErrorException: Catchable Fatal Error: Argument 1 passed to Illuminate\Html\FormBuilder::open() must be of the type array

So I tried Form::open('[clients/' . $client->id). This doesn't generate an error, but now the form open tag is:

<form method="POST" action="http://boekhouding.dev/clients/1/edit" accept-charset="UTF-8" clients/1="clients/1">

And I also tried it like this: Form::open(['action' => 'clients/' . $client->id]) but when I do it like this, the form open tag has no action at all.

So, does anyone know how to set the form action? Using a named route would be perfect, but even being able to set the action at all would be nice.

回答1:

You can use named route, controller action or simple url to set form action.

To set it via named route use:

{{ Form::open(array('route' => array('route_name', $client->id))) }}

To set it via controller action use:

{{ Form::open(array('action' => array('ClientController@update', $client->id))) }}

So the keyword action does not reffer to 'action' parameter of form tag, but to controller action

And you can also use plain URL like this:

{{ Form::open(array('url' => 'someurl')) }}


回答2:

@jeffrey_way tweeted about improving the new FormBuilder in Laravel 4. The following paste bucket link should help. It seems to be more about RESTful controllers, but relavant.

Form action sensible defaults - paste bucket

I thought I read something about him coming out with a Forms tutorial tomorrow. If so, it might be found here net.tutsplus.com/?s=laravel