Advertisement
Guest User

Mojo Routes Question

a guest
Oct 3rd, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #I clearly still don't quite get how to set up hierarchical routes. I've tried this:
  2. my $route = $r->route('/foo')->to('controller' => 'example');
  3. my $foo_route = $route->get->to(action => 'foo');
  4. my $bar_route = $route->post->to(action => 'bar');
  5. # This one works fine. Creates routes:
  6. # /foo *
  7. # +/ GET
  8. # +/ POST
  9.  
  10. #But then, if I try to tack on a subroute:
  11. $bar_route->get('/baz')->to(action => 'bas');
  12. #then I tried to get fancy and chain my routes together like this:
  13. my $route = $r->route('/foo')->to('controller' => 'example')
  14. ->get->to(action => 'foo');
  15. ->post->to(action => 'bar');
  16.  
  17. # This does not work. It creates these routes:
  18. # /foo *
  19. # +/ GET
  20. # +/ GET
  21. # +/baz GET
  22. #
  23. # And they don't work. GET to /foo is functional, but POST to /foo now fails, and GET to /foo/baz doesn't work.
  24. #
  25. # What gives? The initial stab of routing two methods to a single path worked, but doesn't allow me to
  26. # chain anything on.
  27.  
  28. # This version works flawlessly:
  29. $r->get('/foo')->to('example#foo');
  30. $r->post('/foo')->to('example#bar');
  31. $r->get('/foo/baz')->to('example#baz');
  32.  
  33. # But it's no good because of the redundant code - '/foo' is repeated in all 3 routes (2 of which have
  34. # identical paths!), and the controller is repeated across all three of them. I could resolve that by stuffing
  35. # the hardwired strings into variables, but before I do that I'm trying to see if I can get it going with just the
  36. # method calls. If it can't be done, then c'est le vie
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement