{"id":13704042,"url":"https://github.com/NoahBuscher/Macaw","last_synced_at":"2025-05-05T09:32:41.749Z","repository":{"id":12826981,"uuid":"15502262","full_name":"noahbuscher/macaw","owner":"noahbuscher","description":"The simple PHP router","archived":true,"fork":false,"pushed_at":"2020-08-06T12:08:09.000Z","size":70,"stargazers_count":894,"open_issues_count":16,"forks_count":186,"subscribers_count":63,"default_branch":"master","last_synced_at":"2024-11-11T06:37:10.722Z","etag":null,"topics":["php","php-router","router"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noahbuscher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-29T02:21:14.000Z","updated_at":"2024-11-01T02:38:12.000Z","dependencies_parsed_at":"2022-09-07T22:22:14.561Z","dependency_job_id":null,"html_url":"https://github.com/noahbuscher/macaw","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/noahbuscher%2Fmacaw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahbuscher%2Fmacaw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahbuscher%2Fmacaw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahbuscher%2Fmacaw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noahbuscher","download_url":"https://codeload.github.com/noahbuscher/macaw/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224439675,"owners_count":17311500,"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","router"],"created_at":"2024-08-02T21:01:03.411Z","updated_at":"2024-11-13T11:30:40.426Z","avatar_url":"https://github.com/noahbuscher.png","language":"PHP","funding_links":[],"categories":["路由 Routers","目录","路由( Routers )","类库"],"sub_categories":["路由 Routers","路由"],"readme":"Macaw\n=====\n\nMacaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately.\n\n### Install\n\nIf you have Composer, just include Macaw as a project dependency in your `composer.json`. If you don't just install it by downloading the .ZIP file and extracting it to your project directory.\n\n```\nrequire: {\n    \"noahbuscher/macaw\": \"dev-master\"\n}\n```\n\n### Examples\n\nFirst, `use` the Macaw namespace:\n\n```PHP\nuse \\NoahBuscher\\Macaw\\Macaw;\n```\n\nMacaw is not an object, so you can just make direct operations to the class. Here's the Hello World:\n\n```PHP\nMacaw::get('/', function() {\n  echo 'Hello world!';\n});\n\nMacaw::dispatch();\n```\n\nMacaw also supports lambda URIs, such as:\n\n```PHP\nMacaw::get('/(:any)', function($slug) {\n  echo 'The slug is: ' . $slug;\n});\n\nMacaw::dispatch();\n```\n\nYou can also make requests for HTTP methods in Macaw, so you could also do:\n\n```PHP\nMacaw::get('/', function() {\n  echo 'I'm a GET request!';\n});\n\nMacaw::post('/', function() {\n  echo 'I'm a POST request!';\n});\n\nMacaw::any('/', function() {\n  echo 'I can be both a GET and a POST request!';\n});\n\nMacaw::dispatch();\n```\n\nLastly, if there is no route defined for a certain location, you can make Macaw run a custom callback, like:\n\n```PHP\nMacaw::error(function() {\n  echo '404 :: Not Found';\n});\n```\n\nIf you don't specify an error callback, Macaw will just echo `404`.\n\n\u003chr\u003e\n\nIn order to let the server know the URI does not point to a real file, you may need to use one of the example [configuration files](https://github.com/noahbuscher/Macaw/blob/master/config).\n\n\n## Example passing to a controller instead of a closure\n\u003chr\u003e\nIt's possible to pass the namespace path to a controller instead of the closure:\n\nFor this demo lets say I have a folder called controllers with a demo.php\n\nindex.php:\n\n```php\nrequire('vendor/autoload.php');\n\nuse NoahBuscher\\Macaw\\Macaw;\n\nMacaw::get('/', 'Controllers\\demo@index');\nMacaw::get('page', 'Controllers\\demo@page');\nMacaw::get('view/(:num)', 'Controllers\\demo@view');\n\nMacaw::dispatch();\n```\n\ndemo.php:\n\n```php\n\u003c?php\nnamespace Controllers;\n\nclass Demo {\n\n    public function index()\n    {\n        echo 'home';\n    }\n\n    public function page()\n    {\n        echo 'page';\n    }\n\n    public function view($id)\n    {\n        echo $id;\n    }\n\n}\n```\n\nThis is with Macaw installed via composer.\n\ncomposer.json:\n\n```\n{\n   \"require\": {\n        \"noahbuscher/macaw\": \"dev-master\"\n    },\n    \"autoload\": {\n        \"psr-4\": {\n            \"\" : \"\"\n        }\n    }\n}\n````\n\n.htaccess(Apache):\n\n```\nRewriteEngine On\nRewriteBase /\n\n# Allow any files or directories that exist to be displayed directly\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\n\nRewriteRule ^(.*)$ index.php?$1 [QSA,L]\n```\n\n.htaccess(Nginx):\n\n```\nrewrite ^/(.*)/$ /$1 redirect;\n\nif (!-e $request_filename){\n\trewrite ^(.*)$ /index.php break;\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNoahBuscher%2FMacaw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNoahBuscher%2FMacaw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNoahBuscher%2FMacaw/lists"}