我有以下视图(main.php),其中包含发布到同一路由的两个单独的表单:
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form>以下是呈现该视图并发布表单数据的路由逻辑:
$app->get('/main', function() use ($app) { $app->render('main.php'); })->name('main'); $app->post('/main', function() use ($app) { // do something })->name('main.post');现在在我的$app->post方法中,我想区分提交的表单和相应的单独逻辑,如下所示:
$app->post('/main', function() use ($app) { if(first form was submitted) { // do something } else if(second form was submitted){ // do something else } })->name('main.post');我想要完成的工作是否需要利用中间件? 如果没有,我需要实现什么逻辑来完成这个看似简单的任务 ?
I have the following view (main.php) that contains two separate forms that post to the same route:
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form>Here is the route logic that renders that view and also post the form data:
$app->get('/main', function() use ($app) { $app->render('main.php'); })->name('main'); $app->post('/main', function() use ($app) { // do something })->name('main.post');Now within my $app->post method, I would like to differentiate between which form was submitted and do separate logic accordingly, like so:
$app->post('/main', function() use ($app) { if(first form was submitted) { // do something } else if(second form was submitted){ // do something else } })->name('main.post');Does what I am trying to accomplish need to utilize Middleware? if not, what logic do i need to implement to accomplish this seemingly simple task?
最满意答案
您可以在表单中包含一个隐藏字段,指示提交的表单。
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>然后在您的路线中检查which输入的值。
$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');You could include an hidden field in your form which indicates which form was submitted.
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>Then in your route check the value of which input.
$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');更多推荐
发布评论