From 2389d66da849798f8d4ec5f10e3b07c11da49185 Mon Sep 17 00:00:00 2001 From: Michael Francis Date: Sat, 28 May 2011 13:28:16 -0400 Subject: Initial Commit --- config/routes.php | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 config/routes.php (limited to 'config/routes.php') diff --git a/config/routes.php b/config/routes.php new file mode 100644 index 0000000..bde33dd --- /dev/null +++ b/config/routes.php @@ -0,0 +1,149 @@ + 'lithium\test\Controller')); + Router::connect('/test', array('controller' => 'lithium\test\Controller')); +} + + +/* This is the login and logout routes */ +Router::connect('/login', array('controller' => 'users', 'action' => 'login')); +Router::connect('/logout', array('controller' => 'users', 'action' => 'logout')); + +/* Content routes */ +Router::connect('/anime', array('controller' => 'content', 'action' => 'anime')); +Router::connect('/anime/{:args}', array('controller' => 'content', 'action' => 'anime')); + +//Pagination route +Router::connect('/{:controller}/{:action}/page:{:page:[0-9]+}'); + + +/** +* Define an anonymous function that we will pass to the router instead of linking to a controller action +* The logic is quite simple: +* 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. +* 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. +* +* This method is limited, supports few formats etc but its a good start +*/ +$imageSizer = function($request) { + $contentTypeMappings = array( + 'jpg' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif' + ); + // Generate file based image of this + $imageBody = Photo::version($request->id, array( + 'width' => $request->width, + 'height' => $request->height + )); + return new Response(array( + 'headers' => array('Content-type' => $contentTypeMappings[$request->type]), + 'body' => $imageBody + )); +}; + +/** +This is a little bit more complicated. +We state that the handler for when this route is matched is the anonymous function we've declared and +we set up a pattern to match our two cases of image urls — both with and without size information. +The regex is quite simple even if it looks complex: +^/image/ <- begin with /image/ +(?P) is for setting up a named capture group. This equals doing {:foo:{pattern}} in Lithium. +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. + +Im unsure if the keys array can be handled some other way, but it failed for me without it. +*/ +$imageHandlingOptions = array( + 'handler' => $imageSizer, + 'pattern' => '@^/image/(?P[0-9a-f]{24})(_(?P[0-9]*)x(?P[0-9]*)?)\.(?[a-z]{2,4})@', + 'keys' => array('id'=>'id', 'width'=>'width', 'height'=>'height', 'type'=>'type') +); + +/** +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. +*/ +Router::connect('/image/{:id:[0-9a-f]{24}}.jpg', array(), $imageHandlingOptions); + + + +/** + * ### Database object routes + * + * The routes below are used primarily for accessing database objects, where `{:id}` corresponds to + * the primary key of the database object, and can be accessed in the controller as + * `$this->request->id`. + * + * If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key + * is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`, + * `/posts/view/1138.json`, etc. + */ +// Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null)); +// Router::connect('/{:controller}/{:action}/{:id:\d+}'); + +/** + * If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of + * database which uses 24-character hexidecimal values as primary keys, uncomment the routes below. + */ +Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}.{:type}', array('id' => null)); +Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}'); + +/** + * Finally, connect the default route. This route acts as a catch-all, intercepting requests in the + * following forms: + * + * - `/foo/bar`: Routes to `FooController::bar()` with no parameters passed. + * - `/foo/bar/param1/param2`: Routes to `FooController::bar('param1, 'param2')`. + * - `/foo`: Routes to `FooController::index()`, since `'index'` is assumed to be the action if none + * is otherwise specified. + * + * In almost all cases, custom routes should be added above this one, since route-matching works in + * a top-down fashion. + */ +Router::connect('/{:controller}/{:action}/{:args}'); + +?> \ No newline at end of file -- cgit v1.2.3