66479

Can I have a variable number of URI parameters or key-value pairs in Laravel 4?

I've got a shopping cart that I'd like to be able to pass a variable amount of optional parameters. Things like: sort by, filter by, include/exclude etc. So the URL may be:

/products /products/sort/alphabetically /products/filter/cloths /products/socks/true /products/sort/alphabetically/socks/true/hats/false/

Etc.

I suppose I could have a route with placeholders for all of the possible parameters and set default values in the URL, so something like:

Route::get('products/sort/{$sort?}/filter/{$filter?}/socks/{$socks?}/hats/{$hats?}/...', function($sort = 'alphabetically', $filter = false, $socks = true, $hats = true, ...) { ... });

Then for instance to just exclude hats I'd have to have a URL as follow:

/products/sort/alphabetically/filter/false/socks/true/hats/false

But that seems really... inelegant. Is there a good way of doing this? I suppose I could also try to write a server rewrite rule to account for that, but I don't like the idea of circumventing Laravel.

Answer1:

You should use the query string (GET parameters) for filters like those. When you use the query string parameters can be in any order and can be easily skipped if they're not needed. It's also very easy to make a simple form (with method="GET") that can filter your list

With GET parameters a URL would look more like:

/products /products?sort=alphabetically /products?filter=cloths /products?socks=true /products?sort=alphabetically&socks=true&hats=false

GET parameters can then be retrieved individually with Input::get('name', 'default') or as a collection with Input::all(). This is also how the paginator will add the page number to your links.

Recommend

  • Coffeescript: how to link div, but override link if child links are clicked
  • Javascript falsy values (null, undefined, false, empty string: “” or '' and 0) and compari
  • How to know when the VerticalScrollBar is showing?
  • Comparing Strings in Swift 3 gives false results (Linux)
  • Why changing GitHub repository language not working?
  • Why isn't this awk command working
  • un_active button until function finished in kivy
  • What is the best way to implement multiple logs location using log4j?
  • Maximum Flow in Directed Graph
  • How to make sure child object is valid while saving parent?
  • How to sanitize input with PHP and the sqlsrv driver?
  • Can you break a while loop from outside the loop?
  • Psycopg2 string formatting with variable names for type creation
  • pig - transform data from rows to columns while inserting placeholders for non-existent fields in sp
  • Executing a function that adds columns and populates them dependig on other columns in Pandas
  • nodeJS util.format passing an array
  • How to get value from merged-cells in Excel document using OpenXML and C#
  • Disable/remove close icon on Kendo Grid's default group column
  • Plotting Route with Multiple Points in iOS
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Caching attributes in superclass
  • Test if a set exists before trying to drop it
  • Django: Count of Group Elements
  • Modifying destination and filename of gulp-svg-sprite
  • GridView Sorting works once only
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?