簡體   English   中英

Redirect::route with parameter in URL in Laravel 5

[英]Redirect::route with parameter in URL in Laravel 5

我正在開發一個 Laravel 5 應用程序,我有這條路線

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

在我的 controller 中,在我正確撥打電話后,我想使用以下命令重定向到該視圖:

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

問題是我不知道如何傳遞 {id} 參數,它應該在我的 URL 中。

謝謝你。

您可以將路由參數作為第二個參數傳遞給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!!!');

你仍然可以這樣做:

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

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

您可以像這樣通過重定向傳遞 {id} 參數

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

如果路由器包含這樣的:

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

您可以使用以下

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

或者

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

您可以使用 url 和 id 在外部分配一個變量,並在重定向內部調用它。

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

上面的答案已經顯示了多種方法。 只是添加另一種方法來以緊湊的方式實現這一點。

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

按順序傳遞並按相同順序訪問。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM