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

xytpbqjk  于 2022-11-26  发布在  其他
关注(0)|答案(8)|浏览(145)

我正在开发一个Laravel 5应用程序,我有一条路线
Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => 'RegionController@index']);
在我的控制器中,在我正确地进行了post调用之后,我想重定向到那个视图,使用以下命令:

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

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

  • 谢谢-谢谢
nx7onnlm

nx7onnlm1#

您可以将路由参数作为第二个参数传递给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!!!');
idfiyjo8

idfiyjo82#

您仍然可以这样做:

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

4bbkushb3#

在laravel中重定向此URL有几种方法:

1.将URL与全局重定向帮助器函数一起使用

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

1.使用命名路由

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

1.使用控制器动作

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

trnvg8h34#

您可以使用以下

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

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

izj3ouym5#

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

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

5fjcxozz6#

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

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

dluptydi7#

您可以在外部为变量赋值url和id,然后在redirect内部调用它。

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

rdlzhqv98#

上面的答案已经显示了多种方法来做。只是增加了另一种方法来实现在紧凑的方式。

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

按顺序传递和按相同顺序访问。

Route::get('/displayCustomer/{$param1}/{param2}/{param3}','AdminController@displayCustomer')->middleware('auth','admin');

相关问题