Laravel Request getting current path with query string

前端 未结 9 502
萌比男神i
萌比男神i 2020-12-23 13:20

Is there a Laravel way to get the current path of a Request with its query parameters?

For instance, for the URL:

http://www.example.com/one/two?key=         


        
相关标签:
9条回答
  • 2020-12-23 13:41

    Request class doesn't offer a method that would return exactly what you need. But you can easily get it by concatenating results of 2 other methods:

    echo (Request::getPathInfo() . (Request::getQueryString() ? ('?' . Request::getQueryString()) : '');
    
    0 讨论(0)
  • 2020-12-23 13:44

    Laravel 4.5

    Just use

    Request::fullUrl()
    

    It will return the full url

    You can extract the Querystring with str_replace

    str_replace(Request::url(), '', Request::fullUrl())
    

    Or you can get a array of all the queries with

    Request::query()
    

    Laravel >5.1

    Just use

    $request->fullUrl()
    

    It will return the full url

    You can extract the Querystring with str_replace

    str_replace($request->url(), '',$request->fullUrl())
    

    Or you can get a array of all the queries with

    $request->query()
    
    0 讨论(0)
  • 2020-12-23 13:47

    Get the flag parameter from the URL string http://cube.wisercapital.com/hf/create?flag=1

    public function create(Request $request)
    {
    $flag = $request->input('flag');
    return view('hf.create', compact('page_title', 'page_description', 'flag'));
    }
    
    0 讨论(0)
  • 2020-12-23 13:49

    Try to use the following:

    \Request::getRequestUri()
    
    0 讨论(0)
  • 2020-12-23 13:54

    Similar to Yada's answer: $request->url() will also work if you are injecting Illuminate\Http\Request

    Edit: The difference between fullUrl and url is the fullUrl includes your query parameters

    0 讨论(0)
  • 2020-12-23 13:57
    public functin func_name(Request $request){$reqOutput = $request->getRequestUri();}
    
    0 讨论(0)
提交回复
热议问题