routes.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. /**
  9. * The routes file is where you define your URL structure, which is an important part of the
  10. * [information architecture](http://en.wikipedia.org/wiki/Information_architecture) of your
  11. * application. Here, you can use _routes_ to match up URL pattern strings to a set of parameters,
  12. * usually including a controller and action to dispatch matching requests to. For more information,
  13. * see the `Router` and `Route` classes.
  14. *
  15. * @see lithium\net\http\Router
  16. * @see lithium\net\http\Route
  17. */
  18. use lithium\net\http\Router;
  19. use lithium\core\Environment;
  20. use app\models\Photo;
  21. /**
  22. * Here, we are connecting `'/'` (the base path) to controller called `'Pages'`,
  23. * its action called `view()`, and we pass a param to select the view file
  24. * to use (in this case, `/views/pages/home.html.php`; see `app\controllers\PagesController`
  25. * for details).
  26. *
  27. * @see app\controllers\PagesController
  28. */
  29. Router::connect('/', 'Pages::view');
  30. /**
  31. * Connect the rest of `PagesController`'s URLs. This will route URLs like `/pages/about` to
  32. * `PagesController`, rendering `/views/pages/about.html.php` as a static page.
  33. */
  34. Router::connect('/pages/{:args}', 'Pages::view');
  35. /**
  36. * Add the testing routes. These routes are only connected in non-production environments, and allow
  37. * browser-based access to the test suite for running unit and integration tests for the Lithium
  38. * core, as well as your own application and any other loaded plugins or frameworks. Browse to
  39. * [http://path/to/app/test](/test) to run tests.
  40. */
  41. if (!Environment::is('production')) {
  42. Router::connect('/test/{:args}', array('controller' => 'lithium\test\Controller'));
  43. Router::connect('/test', array('controller' => 'lithium\test\Controller'));
  44. }
  45. /* These are the login and logout routes */
  46. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  47. Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
  48. //Pagination route
  49. Router::connect('/{:controller}/{:action}/page:{:page:[0-9]+}');
  50. /**
  51. * Define an anonymous function that we will pass to the router instead of linking to a controller action
  52. * The logic is quite simple:
  53. * Call the version() method on the Photo model class with $request->id (MongoId for image) and a set of options. This is passed as an array to allow adding more options later.
  54. * Finally just return a Response object with the image data as body (this is what version() returns) and the appropriate content type for the file ending.
  55. *
  56. * This method is limited, supports few formats etc but its a good start
  57. */
  58. $imageSizer = function($request) {
  59. $contentTypeMappings = array(
  60. 'jpg' => 'image/jpeg',
  61. 'jpeg' => 'image/jpeg',
  62. 'png' => 'image/png',
  63. 'gif' => 'image/gif'
  64. );
  65. // Generate file based image of this
  66. $imageBody = Photo::version($request->id, array(
  67. 'width' => $request->width,
  68. 'height' => $request->height
  69. ));
  70. return new Response(array(
  71. 'headers' => array('Content-type' => $contentTypeMappings[$request->type]),
  72. 'body' => $imageBody
  73. ));
  74. };
  75. /**
  76. This is a little bit more complicated.
  77. We state that the handler for when this route is matched is the anonymous function we've declared and
  78. we set up a pattern to match our two cases of image urls — both with and without size information.
  79. The regex is quite simple even if it looks complex:
  80. ^/image/ <- begin with /image/
  81. (?P<foo>) is for setting up a named capture group. This equals doing {:foo:{pattern}} in Lithium.
  82. So we have 1 capture group named {id} that have to match by 24 signs (mongoid), and an optional part "_{width}x{height}" and finally the filtype.
  83. I'm unsure if the keys array can be handled some other way, but it failed for me without it.
  84. */
  85. $imageHandlingOptions = array(
  86. 'handler' => $imageSizer,
  87. 'pattern' => '@^/image/(?P<id>[0-9a-f]{24})(_(?P<width>[0-9]*)x(?P<height>[0-9]*)?)\.(?<type>[a-z]{2,4})@',
  88. 'keys' => array('id'=>'id', 'width'=>'width', 'height'=>'height', 'type'=>'type')
  89. );
  90. /**
  91. Finally we connect this as a route. The regex sent as the first param here is overriden by the more complex one we have defined in the options array.
  92. */
  93. Router::connect('/image/{:id:[0-9a-f]{24}}.jpg', array(), $imageHandlingOptions);
  94. /**
  95. * ### Database object routes
  96. *
  97. * The routes below are used primarily for accessing database objects, where `{:id}` corresponds to
  98. * the primary key of the database object, and can be accessed in the controller as
  99. * `$this->request->id`.
  100. *
  101. * If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key
  102. * is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`,
  103. * `/posts/view/1138.json`, etc.
  104. */
  105. // Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null));
  106. // Router::connect('/{:controller}/{:action}/{:id:\d+}');
  107. /**
  108. * If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of
  109. * database which uses 24-character hexidecimal values as primary keys, uncomment the routes below.
  110. */
  111. Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}.{:type}', array('id' => null));
  112. Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}');
  113. /**
  114. * Finally, connect the default route. This route acts as a catch-all, intercepting requests in the
  115. * following forms:
  116. *
  117. * - `/foo/bar`: Routes to `FooController::bar()` with no parameters passed.
  118. * - `/foo/bar/param1/param2`: Routes to `FooController::bar('param1, 'param2')`.
  119. * - `/foo`: Routes to `FooController::index()`, since `'index'` is assumed to be the action if none
  120. * is otherwise specified.
  121. *
  122. * In almost all cases, custom routes should be added above this one, since route-matching works in
  123. * a top-down fashion.
  124. */
  125. Router::connect('/{:controller}/{:action}/{:args}');
  126. ?>