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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30019627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:29:34  来源:igfitidea点击:

Redirect::route with parameter in URL in Laravel 5

phplaravelroutes

提问by Bellots

I'm developing a Laravel 5 app, I have this route

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

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

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

In my controller, after I make a post call correctly, I want to redirect to that view, with this command:

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

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

The problem is that I don't know how can I pass {id} parameter, which should be in my URL.

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

Thank you.

谢谢你。

回答by lukasgeiter

You can pass the route parameters as second argument to route():

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

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

If it's only one you also don't need to write it as array:

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

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

In case your route has more parameters, or if it has only one, but you want to clearly specify which parameter has each value (for readability purposes), you can always do this:

如果您的路由有更多参数,或者只有一个,但您想清楚地指定哪个参数具有每个值(出于可读性目的),您始终可以这样做:

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

回答by Awa Melvine

You could still do it like this:

你仍然可以这样做:

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

In cases where you have multiple parameters, you can pass the parameters as array, for example, say you had to pass the capital of a particular region in you route, your route could look something like this:

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

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

and you can then redirect using:

然后您可以使用以下方法重定向:

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

回答by Satendra Maurya

There are several ways to redirect this URL in laravel:

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

  1. Using URL with a global redirect helper function

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

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

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

回答by S Debasish Nayak

If the router contains like this :

如果路由器包含这样的:

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

and in the controller redirection can be done like this

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

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

回答by learnkevin

You can use the following

您可以使用以下

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

OR

或者

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

回答by BeingCoder's

You can pass {id} parameter with redirection like that

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

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