- 1 year ago 269
PHP Question

Laravel get Current URL parameters and append to new URL Request

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


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.


when I want


Answer Source

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

Laravel offers excellent Routing Features: (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() {...});

Read the docs! ... Sorry for being so direct, but you miss the great features of this framework.

Good beginner books for Laravel 3/4: Code Happy & Code Bright from Dayle Rees (as e-book)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download