{"id":27044109,"url":"https://github.com/math280h/php-router","last_synced_at":"2026-07-14T20:31:55.525Z","repository":{"id":65839118,"uuid":"600776374","full_name":"math280h/php-router","owner":"math280h","description":"Simple PHP Router implementation","archived":false,"fork":false,"pushed_at":"2023-02-16T23:48:57.000Z","size":30,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T11:30:22.706Z","etag":null,"topics":["php","php-library","php-router","php-routing","router"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/math280h/php-router","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/math280h.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-02-12T15:13:00.000Z","updated_at":"2023-02-13T10:52:48.000Z","dependencies_parsed_at":"2023-02-20T21:30:52.649Z","dependency_job_id":null,"html_url":"https://github.com/math280h/php-router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/math280h/php-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/math280h%2Fphp-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/math280h%2Fphp-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/math280h%2Fphp-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/math280h%2Fphp-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/math280h","download_url":"https://codeload.github.com/math280h/php-router/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/math280h%2Fphp-router/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35478682,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-14T02:00:06.603Z","response_time":114,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-library","php-router","php-routing","router"],"created_at":"2025-04-05T05:19:04.162Z","updated_at":"2026-07-14T20:31:55.513Z","avatar_url":"https://github.com/math280h.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-router\n\nSimple PHP Router, it supports features such as middlewares and views using aura/view\n\n\n## Installation\n\n```\ncomposer require math280h/php-router\n```\n\n## Usage\n\nFor simple usage you can inline callback functions directly in the routes as shown below.\n\n```php\nuse Math280h\\PhpRouter\\Router;\nuse Math280h\\PhpRouter\\Request;\n\nsession_start();\n\n$router = new Router(\"./public\");\n$router-\u003eget('/', function () {\n    echo 'Hello World';\n});\n\n$router-\u003epost('/', function (Request $request) {\n    echo 'Hello World';\n});\n\n$router-\u003erun();\n```\n***Note**: The router always passes the Request object to callback functions*\n\n### Accepting different HTTP Methods\n\nThe router exposes a function for each accepted HTTP method. The list of available methods are:\n* GET\n* POST\n* PUT\n* DELETE\n* OPTIONS\n* HEAD\n* PATCH\n\nAnd they can be called like so:\n```php\n$router-\u003eget('/', MyController::class . '::index')\n$router-\u003epost('/', MyController::class . '::index')\n```\n\n### Route groups\n\nYou can group routes together, this allows you to e.g. define a prefix for a colleciton of routes or apply\nmiddleware to the collection of routes.\n\nYou can define a route group like so:\n```php\nuse Math280h\\PhpRouter\\Route;\n\n$router-\u003egroup([\"prefix\" =\u003e \"test\"], [\n    Route::get('/1', function () {\n        echo 'Hello World';\n    }),\n    Route::get('/2', function () {\n        echo 'Hello World';\n    }),\n]);\n```\nThis example results in two routes with the paths `/test/1` and `/test/2`\n\n### Adding middleware\n\nThe router supports middleware than runs after the connection is accepted but before the request\nis forwarded to the callback function.\n\nFor now, only direct callback functions are supported and can be added to the router like so:\n\n```php\n$router-\u003eaddMiddleware(\"log\", function ($request) {\n    print_r($request);\n});\n```\n\nThe router will always pass the request object to the callback function.\n\nOnce the middleware is added to the router you can attach it to your route like so:\n```php\n$router-\u003eget('/', function () {\n    echo 'Hello World';\n}, ['log']);\n```\n\n### Passing callbacks from classes to the router\n\nThe router allows you to pass references to functions inside classes instead of inlining the callback functions\nYou can do this like so:\n\n```php\nclass MyController {\n    public function index($request) {\n        echo 'Hello World';\n    }\n}\n\n$router-\u003eget('/', MyController::class . '::index');\n```\n\n### Returning views\n\nThe router is built with support for aura/view. This allows callback functions to pass back a view\nand the router will automatically render it.\n\nThis can achieved like so:\n```php\nclass MyController {\n    public function index($request) {\n        $view_factory = new \\Aura\\View\\ViewFactory;\n        $view = $view_factory-\u003enewInstance();\n        $view_registry = $view-\u003egetViewRegistry();\n        $layout_registry = $view-\u003egetLayoutRegistry();\n        $layout_registry-\u003eset('default', dirname(__DIR__) . '/views/layouts/default.php');\n        $view_registry-\u003eset('page', dirname(__DIR__) . '/views/my-view.php');\n        $view-\u003esetView('page');\n        $view-\u003esetLayout($layout);\n        return $view;\n    }\n}\n\n$router-\u003eget('/', MyController::class . '::index');\n```\n\nIt's recommended to implement a helper function for spinning up new views so you don't have to duplicate the factory creation in your code\n\n#### View helper function\n\nA view helper function can look something like this:\n\n```php\n/**\n * Returns a view Object\n *\n * @param string $path\n * @param array $data\n * @param string $layout\n * @return View\n */\nfunction view(string $path, array $data = [], string $layout = 'default'): View\n{\n    $view_factory = new \\Aura\\View\\ViewFactory;\n    $view = $view_factory-\u003enewInstance();\n    $view_registry = $view-\u003egetViewRegistry();\n    $layout_registry = $view-\u003egetLayoutRegistry();\n    $layout_registry-\u003eset('default', dirname(__DIR__) . '/views/layouts/default.php');\n    $view_registry-\u003eset('page', dirname(__DIR__) . '/views/' . $path . '.php');\n    $view-\u003esetView('page');\n    $view-\u003esetLayout($layout);\n    $view-\u003eaddData($data);\n    return $view;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmath280h%2Fphp-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmath280h%2Fphp-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmath280h%2Fphp-router/lists"}