【问题标题】:Multiple forms in one page in Laravel 5.1Laravel 5.1 中的一页中的多个表单
【发布时间】:2016-02-03 07:00:59
【问题描述】:

我有一个刀片页面,其中一个要更新,另一个要保存

我的问题如何根据方法类型提交两者

我试图像下面的例子那样实现这一点

 public function postCompanyProfileSettings(Request $request)
 {

    if($request->isMethod('POST')) {
          // do something to save
      }
     if($request->isMethod('PUT')) {
        // do something to update 
     }
 }

使用 POST 方法效果很好,但使用 PUT 返回 Route Exception MethodNotAllowedHttpException in RouteCollection.php line 219:

我认为是 routs.php 中的问题,但我不知道如何处理多个表单(多种方法)的一个路由

我在 route.php 文件中的路线

  //setting routes...
 get('/home/settings', 'CompanyProfileController@getCompanyProfileSettings');
 post('/home/settings','CompanyProfileController@postCompanyProfileSettings');

有什么方法可以实现吗?

【问题讨论】:

  • 你的路由中的 put 方法在哪里?
  • @aldrin27:这是我的问题,我不知道究竟该怎么做才能为多种形式(多种方法)处理一条路线有什么方法可以实现吗?
  • 试试这个:Route::match(['get', 'post', 'put'], '/', function () { return 'Hello World'; }); 匹配您的路线。阅读此laravel.com/docs/5.1/routing
  • @aldrin27:感谢您的帮助,现在它正在工作
  • 没什么大不了 :D 祝你好运

标签: php laravel laravel-5 laravel-5.1 laravel-routing


【解决方案1】:

您也可以使用隐藏输入

 public function postCompanyProfileSettings(Request $request)
 {

    if(isset($request->get('update')) {
          // do something to update
      }
      // do something to save  
 }

还有路线..

post('/home/settings','CompanyProfileController@postCompanyProfileSettings');

【讨论】:

    【解决方案2】:

    在我看来,我将使用与 POST 相同的方法。

    第一种形式:

    <form method="POST" action={{ url('vault/{batch_centre_id}/candidates/{id}', ['form' => '1']) }}>
    

    第二种形式:

    <form method="POST" action={{ url('vault/{batch_centre_id}/candidates/{id}', ['form' => '2']) }}>
    

    在您的行动检查表中:

    if ($request->get('form') == 1) {
    return $request->get('form');
    } else if ($request->get('form') == 2) {
    return $request->get('form');
    }
    
    return result;
    

    因此,只要您标记表单并从控制器验证它们,您就可以在单个页面上拥有无限的表单。

    查看answer

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 2016-03-23
      • 1970-01-01
      • 2016-03-08
      • 2016-03-13
      • 2017-01-05
      • 2015-12-11
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多