在Laravel 5中使用URL中的参数重定向:: route

时间:2015-05-03 21:29:34

标签: php laravel routes

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

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

在我的控制器中,正确拨打电话后,我想使用此命令重定向到该视图:

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

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

谢谢。

6 个答案:

答案 0 :(得分:15)

您可以将路由参数作为第二个参数传递给return \Redirect::route('regions', $id)->with('message', 'State saved correctly!!!');

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

如果它只是一个你也不需要把它写成数组:

Size

如果您的路线有更多参数,或者只有一个参数,但您想明确指定哪个参数包含每个值(出于可读性目的),您可以随时执行此操作:

string strSQL = "INSERT INTO Cats (CatName, Hair, [Size], CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

答案 1 :(得分:5)

你仍然可以这样做:

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!!!');

答案 2 :(得分:0)

您可以使用重定向传递{id}参数

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

答案 3 :(得分:0)

在laravel中有多种方法可以重定向此网址:
 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 :(得分:0)

如果路由器包含以下内容:

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!!!');
    }

答案 5 :(得分:0)

您可以使用以下

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

OR

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