【问题标题】:Redirect::route with parameter in URL in Laravel 5Redirect::route 在 Laravel 5 中的 URL 中带有参数
【发布时间】:2015-07-13 05:15:06
【问题描述】:

我正在开发一个 Laravel 5 应用,我有这条路线

Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => 'RegionController@index']);

在我的控制器中,当我正确地进行后调用后,我想使用以下命令重定向到该视图:

return \Redirect::route('regions')->with('message', 'State saved correctly!!!');

问题是我不知道如何传递 {id} 参数,它应该在我的 URL 中。

谢谢。

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:

    您可以在外部分配一个带有 url 和 id 的变量,并在重定向内部调用它。

    $url = 'view_customer/' . $id;
            return redirect($url)->with('message', "Customer Page");
    

    【讨论】:

      【解决方案2】:

      你仍然可以这样做:

      return redirect()->route('regions', $id)->with('message', 'State saved correctly!!!');
      

      如果您有多个参数,您可以将参数作为数组传递,例如:假设您必须在路线中传递特定区域的首府,您的路线可能如下所示:

      Route::get('states/{id}/regions/{capital}', ['as' => 'regions', 'uses' => 'RegionController@index']);
      

      然后你可以使用重定向:

      return redirect()->route('regions', ['id' => $id, 'capital' => $capital])->with('message', 'State saved correctly!');
      

      【讨论】:

      • 第三个解决了我的问题,谢谢并点赞!
      【解决方案3】:

      在 laravel 中有几种方法可以重定向这个 URL:

      1. 使用带有全局重定向辅助函数的 URL

        return redirect('states/'.$id.'/regions')->with('message', 'State saved correctly!!!');  
        
      2. 使用命名路由

        return redirect()->route('regions', ['id' => $id])->with('message', 'State saved correctly!!!');
        
      3. 使用控制器动作

        return redirect()->action('RegionController@index', ['id' => $id])->with('message', 'State saved correctly!!!');
        

      【讨论】:

        【解决方案4】:

        你可以使用下面的

        return redirect(route('addPost1',['pid',$post->id]));
        

        return redirect(route('addPost1'))->with('pid',$post->id);
        

        【讨论】:

          【解决方案5】:

          如果路由器包含这样的内容:

          Route::get('/displayCustomer/{id}','AdminController@displayCustomer')->middleware('auth','admin');
          

          并且在控制器中重定向可以这样完成

              public function displayCustomer($id){
                  $user = DB::table('customer_infos')->where('customer_id', $id)->first();       
                  return view('admin.DisplayCustomer', compact('user', $user));
              }
          
              public function approveCustomerInvoice(Request $request,$id)
              {
                  $customer = CustomerInfo::find($id);
                  $customer->status = 1;
                  $customer->save();
          
                 return redirect()->action('AdminController@displayCustomer', ['id' => $id])->with('message', 'Customer Invoice Approved!!!');
              }
          

          【讨论】:

            【解决方案6】:

            您可以将路由参数作为第二个参数传递给route()

            return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');
            

            如果只有一个也不需要写成数组:

            return \Redirect::route('regions', $id)->with('message', 'State saved correctly!!!');
            

            如果您的路由有更多参数,或者只有一个,但您想清楚地指定哪个参数具有每个值(为了便于阅读),您可以随时这样做:

            return \Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!');
            

            【讨论】:

            • 如你所见,我的 Redirect::route 已经有一个 'message' 参数。如何添加应该放在 URL 中的 $id 参数以及“消息”参数?
            • @Bellots 在所示示例中,您可以看到 $id 作为第二个参数传入route() 方法。第二个参数用于构建路由。您使用with() 方法添加的任何“参数”实际上只是闪入会话中的数据,供您在重定向时访问。因此,您的完整行将是:return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');
            • 试试这个return redirect('states/'.$id.'/regions')->with(['message' => 'State saved correctly!!!']);
            • @SmithFoto,我不建议这样做。我更喜欢使用数组的东西,因为它增加了可读性。
            【解决方案7】:

            你可以像这样通过重定向传递 {id} 参数

            return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');
            

            【讨论】:

              猜你喜欢
              • 2016-05-23
              • 2015-10-19
              • 1970-01-01
              • 2014-11-13
              • 2020-03-12
              • 2013-10-23
              • 2014-06-08
              • 1970-01-01
              • 2014-12-21
              相关资源
              最近更新 更多