簡體   English   中英

如何在 Laravel 5.2 中訪問控制器中的 url 段

[英]How to access url segment in controller in laravel 5.2

我在 Laravel 5.2 中工作,我想訪問控制器中的 URL 段。 我正在使用

echo  Request::segment(2);

但沒有什么是印刷品。 如何從控制器中的 url 獲取值。

在 Laravel 5.2 中你可以這樣做..

echo request()->segment(2);

request() 是 Laravel 5.2 中提供的幾個輔助函數之一。 它返回當前的請求對象,因此您不需要對類頂部的外觀使用 use 語句。

在 Laravel 7 中,我使用它來獲取段

public function my_function(Request $request )
{
    // By using this, we can get the second segment in route
    // Example: example.com/hh/kk
    
    $segment = $request->segment(2);

    // By using this we will get "kk"
}

暫無
暫無

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

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