{"action":"created","issue":{"url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/144","repository_url":"https://api.github.com/repos/dannyvankooten/AltoRouter","labels_url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/144/labels{/name}","comments_url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/144/comments","events_url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/144/events","html_url":"https://github.com/dannyvankooten/AltoRouter/issues/144","id":146012097,"number":144,"title":"Call class and method and pass parameters using global route","user":{"login":"iamdriz","id":801252,"avatar_url":"https://avatars.githubusercontent.com/u/801252?v=3","gravatar_id":"","url":"https://api.github.com/users/iamdriz","html_url":"https://github.com/iamdriz","followers_url":"https://api.github.com/users/iamdriz/followers","following_url":"https://api.github.com/users/iamdriz/following{/other_user}","gists_url":"https://api.github.com/users/iamdriz/gists{/gist_id}","starred_url":"https://api.github.com/users/iamdriz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamdriz/subscriptions","organizations_url":"https://api.github.com/users/iamdriz/orgs","repos_url":"https://api.github.com/users/iamdriz/repos","events_url":"https://api.github.com/users/iamdriz/events{/privacy}","received_events_url":"https://api.github.com/users/iamdriz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-04-05T14:47:35Z","updated_at":"2016-06-21T21:52:45Z","closed_at":null,"body":"Is it possible to create a global route that will call a class and method and pass any optional parameters (including query strings)?\r\n\r\nSo for example let's say I have the following class acting as a controller and containing methods for actions (like in an MVC pattern). I also want to catch if the action isn't passed and default to the Index method in the controller.\r\n\r\n~~~\r\nclass App\r\n{\r\n\tpublic static function Index()\r\n\t{\r\n\t\techo 'Index!';\r\n\t\tif(isset($_REQUEST['page'])) echo 'Page: ' . $_REQUEST['page'];\r\n\t}\r\n\t\r\n\tpublic static function Create()\r\n\t{\r\n\t\techo 'Create!';\r\n\t}\r\n\t\r\n\tpublic static function Edit($id)\r\n\t{\r\n\t\techo 'Edit! '.$id;\r\n\t}\r\n\t\r\n\tpublic static function Delete($id)\r\n\t{\r\n\t\techo 'Delete! '.$id;\r\n\t}\r\n\t\r\n\tpublic static function View($id)\r\n\t{\r\n\t\techo 'View! '.$id;\r\n\t}\r\n}\r\n~~~\r\n\r\nThen I want the following URLs to all be catch able and hook up to the above class methods:\r\n\r\n~~~\r\nlocalhost/Demo/LearnBuddy/v1/App/\r\nlocalhost/Demo/LearnBuddy/v1/App/?page=2\r\nlocalhost/Demo/LearnBuddy/v1/App/Index/\r\nlocalhost/Demo/LearnBuddy/v1/App/Index/?page=2\r\nlocalhost/Demo/LearnBuddy/v1/App/Create/\r\nlocalhost/Demo/LearnBuddy/v1/App/Edit/22/\r\nlocalhost/Demo/LearnBuddy/v1/App/Delete/22/\r\nlocalhost/Demo/LearnBuddy/v1/App/View/22/\r\nlocalhost/Demo/LearnBuddy/v1/App/View/22/\r\n~~~\r\n\r\nUsing something along the lines of:\r\n\r\n~~~\r\n$router = new AltoRouter();\r\n\r\n// home\r\n$router->map( 'GET', '/', function() {\t\r\n\techo 'home';\r\n});\r\n\r\n// call the index if no action is passed\r\n$router->map( 'GET', '/[a:controller]/', function( $controller ) {\t\r\n\tif( method_exists( $controller, $action ) )\r\n\t\t$controller::Index();\r\n\telse\r\n\t\techo 'missing';\r\n});\r\n\r\n// call controller and action and pass any parameters\r\n$router->map( 'GET', '/[a:controller]/[a:action]/[a:params]', function( $controller, $action, $params ) {\r\n\tif( method_exists( $controller, $action ) )\r\n\t\t$controller::$action($params);\r\n\telse\r\n\t\techo 'missing';\r\n});\r\n\r\n$match = $router->match();\r\n\r\nvar_dump($match);\r\n\r\n// call closure or throw error\r\nif( $match && is_callable( $match['target'] ) )\r\n{\r\n\tcall_user_func_array( $match['target'], $match['params'] ); \r\n}\r\nelse\r\n{\r\n\techo 'error';\r\n}\r\n~~~\r\n\r\nSo in short, how can I amend the above code to catch controller and action with optional params and query strings? Currently I'm just getting `bool(false)` when I `var_dump($match);`.\r\n\r\nThis is on a Windows machine running IIS (the web config seems to fine, as all URLs are hitting the index.php file so I don't think that is causing none of them to work.)."},"comment":{"url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/comments/227583925","html_url":"https://github.com/dannyvankooten/AltoRouter/issues/144#issuecomment-227583925","issue_url":"https://api.github.com/repos/dannyvankooten/AltoRouter/issues/144","id":227583925,"user":{"login":"koenpunt","id":351038,"avatar_url":"https://avatars.githubusercontent.com/u/351038?v=3","gravatar_id":"","url":"https://api.github.com/users/koenpunt","html_url":"https://github.com/koenpunt","followers_url":"https://api.github.com/users/koenpunt/followers","following_url":"https://api.github.com/users/koenpunt/following{/other_user}","gists_url":"https://api.github.com/users/koenpunt/gists{/gist_id}","starred_url":"https://api.github.com/users/koenpunt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koenpunt/subscriptions","organizations_url":"https://api.github.com/users/koenpunt/orgs","repos_url":"https://api.github.com/users/koenpunt/repos","events_url":"https://api.github.com/users/koenpunt/events{/privacy}","received_events_url":"https://api.github.com/users/koenpunt/received_events","type":"User","site_admin":false},"created_at":"2016-06-21T21:52:45Z","updated_at":"2016-06-21T21:52:45Z","body":"I tested your code, and I'm quite sure it has to do with the trailing slash. One of your routes is defined with, and one without, and AltoRouter is quite strict on that. So please make sure you request the URL with a trailing slash OR remove the trailing slash from the route definition."}}
{"actor":{"display_login":"koenpunt"}}