Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get Current URL parameters and append to new URL Request

Tags:

php

laravel-4

In my Laravel app I have created a sidebar where I have links like

/buy?state=NY or /buy?area=Queens to let the user select a State or Area

I also have a form that lets Users Filter various things like rating, genre. When i click on the Filter button, the url changes to something like

buy?min_year=1880&max_year=2019&min_rating=1&max_rating=10&genre=horror

it drops the ?state=NY or ?area=Queens from the URL

I want to I change in the action to append the current URL parameters to the Filter string

I have tried

$url = Request::path();

if (isset($_GET["state"]) && !empty($_GET['state'])) {
$state = $_GET['state'];
$url = $url . "&state=". $state;

}
if (isset($_GET["council"]) && !empty($_GET['council'])) {
$council = $_GET['council'];
$url = $url . "&council=". $council;

}
if (isset($_GET["area"]) && !empty($_GET['area'])) {
$area = $_GET['area'];
$url = $url . "&area=". $area;

And then in my filter form

{{ Form::open(array('url' => $url, 'class' => 'form-inline', 'method' => 'GET')) }}

but this results in the form returning this string.

/buystate=NY?min_year=1880&max_year=2019&min_rating=1&max_rating=10&genre=horror

when I want

/buy?state=NY&min_year=1880&max_year=2019&min_rating=1&max_rating=10&genre=horror
like image 663
seek.estate Avatar asked Apr 13 '14 09:04

seek.estate


3 Answers

This will solve your problem, and I do agree that we're not supposed to be limited of the framework.

public static function get_full_route() {
    $split_url = explode('/', URL::full());
    $ret_url = [];
    for($i = 0; $i < count($split_url); $i++) if(strlen($split_url[$i]) > 0) $ret_url[] = $split_url[$i];
    return array("protocol" => $ret_url[0], "domain" => $ret_url[1], "route" => $ret_url[2]);
}

Return

// In my case
array(3) { 
   ["protocol"]=> string(5) "http:" 
   ["domain"]=> string(14) "localhost:8000" 
   ["route"]=> string(9) "login?a=1" 
}
like image 123
dud3 Avatar answered Nov 16 '22 08:11

dud3


The accepted answer didn't help me so here is my version based on your example for reference. Note this is with Laravel 5.2 not Laravel 4 as indicated in the question. Note sure if it will work with Laravel 4.

First get the parameters form the current url.

$parameters = request( )->input( ); //return all GET parameters in an array.

You then add or edit what you want.

$parameters['state'] = $state; //this will set or override the 'state' value.

Push all the parameters to your desired route.

$url = route('search', $parameters); 

Any parameters that are not matched in the route url are appended to the end of the url.

Happy dayz. :D

like image 3
Shane Avatar answered Nov 16 '22 08:11

Shane


This is for Laravel 4.x only!

If I understand you well, this is what you need.

Laravel offers excellent Routing Features:

laravel.com/docs/routing (really worth reading!)

From the docs (adapted to your vars)

// Route::post(.... for forms)
Route::get('buy/{state}/{min_year}/{and_so_on}', function($state, $min_year, $and_so_on) {
   return array(
       'state' => $state,
       'min_year' => $min_year,
       'and_so_on' => $and_so_on
   );
});

The HTML part would then be something like

<a href="buy/jersey/2012/and_so_on">Choose State</a>

You may also group routings like so:

Route::group(array('prefix' => '/state'), function(){
    Route::get('/{min_year}'), function() {...});
    Route::get('/{and_so_on}'), function() {...});
});
like image 2
toesslab Avatar answered Nov 16 '22 08:11

toesslab