{"id":13405058,"url":"https://github.com/scrawler-labs/router","last_synced_at":"2025-04-05T09:06:28.280Z","repository":{"id":37006647,"uuid":"90492398","full_name":"scrawler-labs/router","owner":"scrawler-labs","description":"An Fully Automatic RESTful PHP Router","archived":false,"fork":false,"pushed_at":"2025-02-03T10:29:17.000Z","size":316,"stargazers_count":54,"open_issues_count":2,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-19T12:14:35.876Z","etag":null,"topics":["fastroute","php","rest-api","restful","restful-api","router","routing","torophp"],"latest_commit_sha":null,"homepage":"https://component.scrawlerlabs.com/router/","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/scrawler-labs.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}},"created_at":"2017-05-06T21:33:24.000Z","updated_at":"2024-11-08T11:27:47.000Z","dependencies_parsed_at":"2023-01-17T12:42:37.470Z","dependency_job_id":"92c5325d-733f-455a-b58d-62f5d01954c4","html_url":"https://github.com/scrawler-labs/router","commit_stats":{"total_commits":123,"total_committers":7,"mean_commits":"17.571428571428573","dds":0.09756097560975607,"last_synced_commit":"0e87285223644c17f418ac2f72a01553b837d995"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scrawler-labs%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scrawler-labs%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scrawler-labs%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scrawler-labs%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scrawler-labs","download_url":"https://codeload.github.com/scrawler-labs/router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312077,"owners_count":20918344,"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":["fastroute","php","rest-api","restful","restful-api","router","routing","torophp"],"created_at":"2024-07-30T19:01:55.158Z","updated_at":"2025-04-05T09:06:28.225Z","avatar_url":"https://github.com/scrawler-labs.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003ch1\u003e Scrawler Router \u003c/h1\u003e\n\n\u003ca href=\"https://github.com/scrawler-labs/router/actions/workflows/main.yml\"\u003e\u003cimg alt=\"GitHub Workflow Status\" src=\"https://img.shields.io/github/actions/workflow/status/scrawler-labs/router/main.yml?style=flat-square\"\u003e\n\u003c/a\u003e\n[![Codecov](https://img.shields.io/codecov/c/gh/scrawler-labs/router?style=flat-square)](https://app.codecov.io/gh/scrawler-labs/router)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/quality/g/scrawler-labs/router?style=flat-square)](https://scrutinizer-ci.com/g/scrawler-labs/router/?branch=main)\n\u003ca href=\"[https://github.com/scrawler-labs/router/actions/workflows/main.yml](https://github.com/scrawler-labs/app/actions/workflows/main.yml)\"\u003e\u003cimg src=\"https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat-square\" alt=\"PHPStan Enabled\"\u003e\u003c/a\u003e\n[![Packagist Version](https://img.shields.io/packagist/v/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)\n[![Packagist Downloads](https://img.shields.io/packagist/dt/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)\n[![Packagist License](https://img.shields.io/packagist/l/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)\n\u003cbr\u003e\u003cbr\u003e\n\n\n🔥An Fully Automatic, Framework independent, RESTful PHP Router component🔥\u003cbr\u003e\n🇮🇳 Made in India 🇮🇳\n\u003c/div\u003e\n\n![Demo](http://g.recordit.co/lvQba4mnyB.gif)\n\n\nComplete docs can be found [here](https://component.scrawlerlabs.com/router/)\n\n## 🤔 Why use Scrawler Router?\n- Fully automatic, you dont need to define single manual route.\n- Support manual route defination for your edge use case.\n- No configrations , works out of the box with any php project.\n- Stable and well tested.\n- Saves lot of time while building RESTful applications\n  \u003cbr\u003e\u003cbr\u003e\n\n## 💻 Installation\nYou can install Scrawler Router via Composer. If you don't have composer installed , you can download composer from [here](https://getcomposer.org/download/)\n\n```sh\ncomposer require scrawler/router\n```\n\n## ✨ Setup\n\nNote 4.x release changes the way router handles request and response, if you still wanna continue using old way with symfony components goto [3.x branch](https://github.com/scrawler-labs/router/tree/3.x) \n\n```php\n\u003c?php\n\nuse Scrawler\\Router\\Router;\n\n$dir = '/path/to/your/controllers';\n$namespace = 'Namespace\\of\\your\\controllers';\n\n$router = new Router();\n// Register your directory for automatic routing\n$router-\u003eregister($dir,$namespace);\n\n/**\n* you can now also enable route caching by passing your own PSR 16 implementation\n* $cache = new Psr\\SimpleCache\\CacheInterface();\n* $router-\u003eenableCache($cache);\n**/\n\n// Fetch method and URI from somewhere\n$httpMethod = $_SERVER['REQUEST_METHOD'];\n$uri = $_SERVER['REQUEST_URI'];\n\n// Strip query string (?foo=bar) and decode URI\nif (false !== $pos = strpos($uri, '?')) {\n    $uri = substr($uri, 0, $pos);\n}\n$uri = rawurldecode($uri);\n\n//Dispatch route and get back the response\n[$status,$handler,$args,$debug] = $router-\u003edispatch($httpMethod,$uri);\nswitch ($status){\n  case \\Scrawler\\Router\\Router::NOT_FOUND:\n    //handle 404 error\n    // $debug contains extra debug info useful to check failure in automatic routing\n    break;\n  case \\Scrawler\\Router\\Router::METHOD_NOT_ALLOWED:\n    //handle 405 method not allowed\n    break;\n  case \\Scrawler\\Router\\Router::FOUND:\n    //call the handler\n    $response = call_user_func($handler,...$args);\n    // Send Response\n    //echo $response\n}\n\n```\n\nDone now whatever request occurs it will be automatically routed . You don't have define a single route\n\u003cbr\u003e\u003cbr\u003e\n\n## ✏️ Manual routing\nInformation on manual routing can be found in [docs](https://component.scrawlerlabs.com/router/)\n\u003cbr\u003e\u003cbr\u003e\n\n\n## 🦊 How it Works?\n\nThe automatic routing is possible by following some conventions. Lets take a example lets say a controller Hello\n\n```php\n\u003c?php\n//Hello.php\n\nclass Hello\n{\n    public function getWorld()\n    {\n        return \"Hello World\";\n    }\n}\n```\nnow calling `localhost/hello/world` from your browser you will see `hello world` on your screen.\n\u003cbr\u003e\u003cbr\u003e\n\n## 🔥 How does it do it automatically?\n\nEach request to the server is interpreted by Scrawler Router in following way:\n\n`METHOD    /controller/function/arguments1/arguments2`\n\nThe controller and function that would be invoked will be\n\n```php\n\u003c?php\n\nclass Controller\n{\n    public function methodFunction($arguments1, $arguments2)\n    {\n        //Definition goes here\n    }\n}\n```\nFor Example the following call:\n\n`GET  /user/find/1`\n\nwould invoke following controller and method\n\n```php\n\u003c?php\n\nclass User\n{\n    public function getFind($id)\n    {\n        //Function definition goes here\n    }\n}\n```\nIn above example `1` will be passed as argument `$id`\n\u003cbr\u003e\u003cbr\u003e\n\n## ⁉️ How should I name my function for automatic routing?\n\nThe function name in the controller should be named according to following convention:\n`methodFunctionname`\nNote:The method should always be written in small and the first word of function name should always start with capital.\nMethod is the method used while calling url. Valid methods are:\n\n```\nall - maps any kind of request method i.e it can be get,post etc\nget - mpas url called by GET method\npost - maps url called by POST method\nput - maps url called by PUT method\ndelete - maps url called by DELETE method\n```\nSome eg. of \u003cb\u003evalid\u003c/b\u003e function names are:\n```\ngetArticles, postUser, putResource\n```\n\u003cb\u003eInvalid\u003c/b\u003e function names are:\n```\nGETarticles, Postuser, PutResource\n```\n\u003cbr\u003e\n\n## 🏠 Website home page\nScrawler Router uses a special function name `allIndex()` and special controller name `Main`. So If you want to make a controller for your landing page `\\` the controller will be defines as follows\n```php\n// Inside main.php\nclass Main\n{\n    // All request to your landing page will be resolved to this controller\n    // ALternatively you can use getIndex() to resolve only get request\n    public function allIndex()\n    {\n    }\n}\n```\n\u003cbr\u003e\n\n## 🌟 Main Controller\nClass name with `Main` signifies special meaning in Scrawler Router , if you wanna define pages route URL you can use main controler\n```php\n// Inside main.php\nclass Main\n{\n    // Resolves `/`\n    public function getIndex()\n    {\n    }\n    \n    // Resolves `/abc`\n    public function getAbc()\n    {\n    \n    }\n    \n    // Resolves `/hello`\n    public function getHello()\n    {\n    \n    }\n}\n```\n\u003cbr\u003e\n\n## 👉 Index function\nJust like `Main` controller `allIndex(), getIndex(), postIndex()` etc signifies a special meaning , urls with only controller name and no function name will try to resolve into this function.\n```php\n// Inside hello.php\nclass Hello\n{\n    // Resolves `/hello`\n    public function getIndex()\n    {\n    \n    }\n    \n    // Resolves `/hello/abc`\n    public function getAbc()\n    {\n    \n    }\n}\n```\n\u003cbr\u003e\n\n\n## 👏 Supporters\nIf you have reached here consider giving a star to help this project ❤️\n[![Stargazers repo roster for @scrawler-labs/router](https://reporoster.com/stars/dark/notext/scrawler-labs/router)](https://github.com/scrawler-labs/router/stargazers)\n\nThank You for your forks and contributions \n[![Forkers repo roster for @scrawler-labs/router](https://reporoster.com/forks/dark/notext/scrawler-labs/router)](https://github.com/scrawler-labs/router/network/members)\n\u003cbr\u003e\u003cbr\u003e\n\n## 🖥️ Server Configuration\n\n#### Apache\n\nYou may need to add the following snippet in your Apache HTTP server virtual host configuration or **.htaccess** file.\n\n```apacheconf\nRewriteEngine on\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond $1 !^(index\\.php)\nRewriteRule ^(.*)$ /index.php/$1 [L]\n```\n\nAlternatively, if you’re lucky enough to be using a version of Apache greater than 2.2.15, then you can instead just use this one, single line:\n```apacheconf\nFallbackResource /index.php\n```\n\n#### IIS\n\nFor IIS you will need to install URL Rewrite for IIS and then add the following rule to your `web.config`:\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cconfiguration\u003e\n    \u003csystem.webServer\u003e\n        \u003crewrite\u003e\n            \u003crule name=\"Toro\" stopProcessing=\"true\"\u003e\n                \u003cmatch url=\"^(.*)$\" ignoreCase=\"false\" /\u003e\n                \u003cconditions logicalGrouping=\"MatchAll\"\u003e\n                    \u003cadd input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" ignoreCase=\"false\" negate=\"true\" /\u003e\n                    \u003cadd input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" ignoreCase=\"false\" negate=\"true\" /\u003e\n                    \u003cadd input=\"{R:1}\" pattern=\"^(index\\.php)\" ignoreCase=\"false\" negate=\"true\" /\u003e\n                \u003c/conditions\u003e\n                \u003caction type=\"Rewrite\" url=\"/index.php/{R:1}\" /\u003e\n            \u003c/rule\u003e\n        \u003c/rewrite\u003e\n    \u003c/system.webServer\u003e\n\u003c/configuration\u003e\n```\n\n#### Nginx\n\nUnder the `server` block of your virtual host configuration, you only need to add three lines.\n```conf\nlocation / {\n  try_files $uri $uri/ /index.php?$args;\n}\n```\n\n## 📄 License\n\nScrawler Router is created by [Pranjal Pandey](https://www.physcocode.com) and released under\nthe MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrawler-labs%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscrawler-labs%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrawler-labs%2Frouter/lists"}