{"issue":{"user":{"id":1218902,"login":"RVN-BR","url":"https://api.github.com/users/RVN-BR","avatar_url":"https://secure.gravatar.com/avatar/e33af81d39f14364d23b04a622ce8594?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"e33af81d39f14364d23b04a622ce8594"},"milestone":null,"created_at":"2011-12-21T01:04:38Z","title":"API Versioning with Restler v2.x","updated_at":"2011-12-21T02:02:29Z","labels":[],"state":"closed","id":2620467,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"html_url":"https://github.com/Luracast/Restler/issues/23","closed_at":"2011-12-21T01:05:30Z","number":23,"body":"A couple of people talked about versioning so I thought i'd add my way of doing it so far.  It has worked great and no issues that I have come accross.  please do give feedback and let me know if you have any suggestions for improvement:\r\n\r\nbasic structure:\r\n\r\n`\r\nWEBROOT/\r\nWEBROOT/API/\r\nWEBROOT/API/lib/ (all restler files here, shared for any version of API)\r\nWEBROOT/API/index.php (main router)\r\nWEBROOT/API/v1/* (all files for version 1)\r\nWEBROOT/API/v2/* (etc, for version 2)\r\n`\r\n\r\nNote: each version directory includes its own \"routes\" file.  Otherwise I was unable to add/remove functions from one version to the next of the API.\r\n\r\nExample index.php:\r\n\r\n```php\r\n<?php\r\n\r\n$DefaultVersion = \"1\"; \t\t\t// Default production version when called from the \"public\" url.  REST dictates you should include the version from what I know, but I am maintaining a default URL.\r\n$Request_Split = explode(\"/\",$_SERVER['REQUEST_URI']); \t// Split request URI\r\n$Version = $Request_Split[2].'/'; \t\t\t\t// In my case the URIs are /API/vXX/ so Version is the 2nd argument.  This should be changed for some sort of route map such as /API/:version or something…\r\nif (($Version[0] == \"v\") && is_numeric($Version[1])){ \t// Basic checks so we don't try and interpret some injection (../..) nor interpret method calls as API version (this requires version to start with \"v[somenumber]\", otherwise assumes default version.\r\n\tset_include_path($Version);\r\n}else{\t\t\t\t\t\t\t\t// no version specified, use default one… this way we can support API/v1/call and API/call\r\n\tset_include_path(\"v\".$DefaultVersion);\r\n\t$Version = '';\r\n}\r\n\r\n/*\r\n*  Whatever common files you want to include, it may be best to move these into the routes file...\r\n*\r\n******require_once 'NewLibs/Base/LogLib.php';\r\n\r\n******require_once 'NewLibs/Base/DBLib.php'; \r\n*/\r\n\r\nrequire_once 'lib/restler.php';\r\n\r\n#set autoloader\r\n#do not use spl_autoload_register with out parameter\r\n#it will disable the autoloading of formats\r\nspl_autoload_register(\"spl_autoload\");\r\n\r\n$r = new Restler();\r\n$r->setSupportedFormats('JsonFormat', 'XmlFormat');\r\n\r\nrequire_once 'routes.php';\r\n\r\n$r->addAuthenticationClass('Authentication');\r\n\r\n\r\n$r->handle();\r\n```\r\n\r\nAnd now for routes.php (repeat this file inside each application version directory:\r\n\r\n```php\r\n<?php\r\nrequire_once \"NewLibs/Base/newversion.php\";\r\n\r\n$r->addAPIClass('someroute',$Version.'someroute');\r\n$r->addAPIClass('teste',$Version.'teste');  // BEWARE TO HAVE EXACTLY THE SAME NAMES IN BOTH... dont mix upper/lower, etc.. make it identical!  An idea may be to use an array, but I just make sure when I add the routes... (an array and a foreach to addapiclass using the same array element so it is passed exactly the same).\r\n```\r\n\r\nI hope this is of use to someone... just wanted to give a bit back ;)\r\n","url":"https://api.github.com/repos/Luracast/Restler/issues/23","comments":3,"assignee":null},"comment":{"user":{"id":271677,"login":"electrical","url":"https://api.github.com/users/electrical","avatar_url":"https://secure.gravatar.com/avatar/8a78f62134e359e72fe8f95a0b7a6ffd?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"8a78f62134e359e72fe8f95a0b7a6ffd"},"created_at":"2011-12-21T02:02:29Z","updated_at":"2011-12-21T02:02:29Z","id":3228519,"body":"in your code you typed\r\n\r\n```php\r\nrequire_once 'routes.php';\r\n```\r\n\r\nwhile you stated that every version has it's own routes.php file.\r\nso i would assume something like\r\n\r\n```php\r\nrequire_once $Version.'/routes.php';\r\n```","url":"https://api.github.com/repos/Luracast/Restler/issues/comments/3228519"},"action":"created"}