{"id":17812318,"url":"https://github.com/nmicht/simplephprouter","last_synced_at":"2025-08-01T13:35:55.015Z","repository":{"id":148540451,"uuid":"133667667","full_name":"nmicht/SimplePHPRouter","owner":"nmicht","description":"A real simple PHP Router","archived":false,"fork":false,"pushed_at":"2018-12-13T23:48:12.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T21:45:40.626Z","etag":null,"topics":["php","php-router"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nmicht.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-16T13:11:25.000Z","updated_at":"2019-01-10T21:23:52.000Z","dependencies_parsed_at":"2023-05-20T11:15:12.363Z","dependency_job_id":null,"html_url":"https://github.com/nmicht/SimplePHPRouter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nmicht%2FSimplePHPRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nmicht%2FSimplePHPRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nmicht%2FSimplePHPRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nmicht%2FSimplePHPRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nmicht","download_url":"https://codeload.github.com/nmicht/SimplePHPRouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246770157,"owners_count":20830820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","php-router"],"created_at":"2024-10-27T15:59:03.084Z","updated_at":"2025-04-02T07:21:16.973Z","avatar_url":"https://github.com/nmicht.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple PHP Router\n\nA real simple PHP Router for PHP 7.1+ inspired on [PHP Router](https://github.com/dannyvankooten/PHP-Router) and [Klein.php](https://github.com/klein/klein.php)\n\n## Features\n\n- Pre defined regular expression routing.\n- Flexible to add new custom regular expressions.\n- Use URL segments as parameters.\n- Get request data and processes as parameters.\n- Pass parameters to the controller.\n- Get child routes from the current url (optional).\n\n## Usage\n\n### Router in action\nIn order to have the Router working, you just need to instantiate the Router and add all your Routes to it.\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse SimplePHPRouter\\Router;\nuse SimplePHPRouter\\Route;\n\n// Load routes from a file.\n$router = new Router();\n\n// Add routes.\n$router-\u003eaddRoute('/', [Controllers\\Controller::class, 'myAction']);\n$router-\u003eaddRoute('/news/[s:slug]', [Controllers\\Controller::class, 'myAction']);\n$router-\u003eaddRoute('/news/[i:id]', [Controllers\\Controller::class, 'myAction'], 'POST');\n\n// Execute router.\n$r = $router-\u003erun();\n```\n\nIn case the request match with a route, the `run()` method will return the Route object and execute the callback defined on the Route.\n\n### Load routes from a yaml file\nIn some cases, is better for maintenance and scalability to keep routes outside the code, so this is a method to initialize the Router with a set of rules defined on a yaml file.\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse SimplePHPRouter\\Router;\nuse SimplePHPRouter\\Route;\n\n// Load routes from a file.\n$router = Router::loadFromFile('routes.yaml');\n\n// Get controller\n$controller = new Controller();\n\n// You can even add more routes\n$router-\u003eaddRoute('/another/[s:slug]', [Controllers\\Controller::class, 'myAction']);\n\n// Execute router.\n$r = $router-\u003erun();\n```\n\nThe format for the yaml file should be as follow\n```yaml\nroutes:\n  - [/index, someClass, indexAction, GET]\n  - [/contact, someClass, contactAction, POST]\n  - [/about, someClass, aboutAction, GET]\n```\n\n### Controllers in action\n\nThe controller classes can be on any namespace and be build as you need.  \nThe Router have the ability to pass to the controller methods the Request object.\n\n```php\nnamespace Controllers;\n\nuse SimplePHPRouter\\Request;\n\nclass Controller\n{\n    public function myAction(Request $req)\n    {\n        // include here all your logic according the route matched.\n    }\n}\n```\n\n### Add new custom regular expressions\n\nTo make flexible the router, you can create your own regular expressions and add them to the Validator class.  \nMake sure to include a identifier and the regex in the MATCH_TYPES array.\n\n```\n/**\n * The regex for match types on routes.\n * @var array\n */\nconst MATCH_TYPES = [\n    'i'  =\u003e '[0-9]++',\n    'a'  =\u003e '[0-9A-Za-z]++',\n    'h'  =\u003e '[0-9A-Fa-f]++',\n    's'  =\u003e '[0-9A-Za-z-_\\-]++',\n    '*'  =\u003e '.+?',\n    '**' =\u003e '.++',\n    ''   =\u003e '[^/\\.]++'\n];\n```\n\n### Get the child routes from the current URL\n\nThis will return an array with the different sub routes.\nSimilar to the getRouteParts method but this one retrieve more information.\n\n```\n// Get a key -\u003e value array with 'url' and 'name' where name is a properly formatted\n// slug value from the URL\nRouter::getChildRoutes();\n\n//We can override the home name by passing a parameter\nRouter::getChildRoutes('Home');\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnmicht%2Fsimplephprouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnmicht%2Fsimplephprouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnmicht%2Fsimplephprouter/lists"}