0

I am using laravel for the first time and need some help understanding routes. I am setting up a view that will show a list of orders placed by customers. Above this list are 2 search boxes. One for searching by ID, the other for selecting a date. I'd like to have a "default" route so when id/date are NOT included in the route, we see all orders placed so far today.

Routes should be as follows:

  • orders - Should display all orders placed today.
  • orders/{id} - Should show only the specific order that belongs to that id.
  • orders/{date} - Should show all orders placed on a specific date.

{id} and {date} should be distinguished by regular expressions.

I can get any one of the routes to work by themselves but when I try to create the routes for all 3 and modify my controller accordingly I break the others. One example is this:

Route::get('orders/{id}', 'OrderController@getOrders')->where('id', '[0-9]+');

Which works for getting the order by ID, but if I wanted to allow for dates as well I would have to change the route entirely. From that point I'd like to be able to go even further, for example the route orders/12345/edit should bring me to the view that allows me to edit the order.

How can I properly setup my routes/controller to do this?

Laurence
  • 58,936
  • 21
  • 171
  • 212
user2391788
  • 93
  • 1
  • 5
  • Make a route orders/{param} and then check whether the param is an ID or a date. Then call the controller accordingly. But a better solution would be to simply change the route.. – cen Dec 30 '13 at 20:41
  • I feel I can get it working the way you first mentioned. But it seems messy. You say there's a better solution, can you explain? – user2391788 Dec 30 '13 at 20:45
  • Yes, it is messy indeed. That's why I said to just make the routes different. For example, orders/date/{date} and orders/{id} – cen Dec 30 '13 at 20:49
  • why you need to put all our logic into one route ?? – ahmed hamdy Dec 30 '13 at 20:49

2 Answers2

1

Unless you manage to write a regular expression that validates dates or numeric values you have two options:

  1. Write two different routes: one that validates dates and other that validates IDs. Both would point to different methods in the controller.
  2. Use one route that doesn't validate its the parameter and that points to one method in the controller where the type of parameter would be checked for date or ID.

I like the first option better, because I believe both routes are similar yet very different.

EDIT If you want to use the same form to target to different urls depending on the contents of inputs you have to use javascript, you can change the action in the form using:

$('#form').attr('action', "the_url");

And you'd have to set up a listener for the inputs to know which url to point to: Detecting input change in jQuery?

I hope this helps you!

Community
  • 1
  • 1
edpaez
  • 1,593
  • 1
  • 12
  • 22
  • I went with the first option. It's working well, the only issue I am having now is my 2 search boxes. How can I make the form so that when I enter an ID/Date in one of the 2 input boxes it routes me to the correct place. For example, if I enter 123 in the ID box and hit GO it should route me to orders/123. Not orders?id=123 like it is doing when I open the form like so: `{{ Form::open(array('url' => 'orders/', 'method' => 'get')) }}` – user2391788 Dec 31 '13 at 16:51
  • I thought about using javascript, but wanted to avoid it. I ended up making my form post to "orders" and then handled the route like so `Route::post('orders', function() { $search = Input::get('id') ? Input::get('id') : Input::get('date'); return Redirect::to('orders/' . $search); });` – user2391788 Dec 31 '13 at 21:55
1

just make three routes like laravel documentation

orders route:

Route::get('orders', 'OrderController@getOrders');

orders by id route:

   Route::get('orders/{id}','OrderController@getOrdersById')->where('id', '[0-9]+');

orders by data route:

Route::get('orders/{data}', 'OrderController@getOrdersByData')->where('name', '[A-Za-z]+');

also you can create three route into your OrderController like documentation

ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
  • Thank you for your examples. This is close to what I ended up doing, except that I only needed 2 routes. See my comment on edpaez's answer. – user2391788 Dec 31 '13 at 16:53