{"id":17358751,"url":"https://github.com/alexkratky/routex","last_synced_at":"2025-03-27T16:17:35.498Z","repository":{"id":56944622,"uuid":"206128598","full_name":"AlexKratky/RouteX","owner":"AlexKratky","description":"The fast routing system","archived":false,"fork":false,"pushed_at":"2020-05-14T09:11:17.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-03T12:46:49.817Z","etag":null,"topics":["panx-framework","php","route","routex","routing","routing-engine","routing-system"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/alexkratky/routex","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/AlexKratky.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}},"created_at":"2019-09-03T17:00:12.000Z","updated_at":"2021-12-14T02:06:19.000Z","dependencies_parsed_at":"2022-08-21T07:50:52.263Z","dependency_job_id":null,"html_url":"https://github.com/AlexKratky/RouteX","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexKratky%2FRouteX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexKratky%2FRouteX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexKratky%2FRouteX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexKratky%2FRouteX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexKratky","download_url":"https://codeload.github.com/AlexKratky/RouteX/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245878924,"owners_count":20687297,"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":["panx-framework","php","route","routex","routing","routing-engine","routing-system"],"created_at":"2024-10-15T19:06:55.854Z","updated_at":"2025-03-27T16:17:35.475Z","avatar_url":"https://github.com/AlexKratky.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RouteX\n\nThe fast routing system. Easy to use.\n\n### Installation\n\n`composer require alexkratky/routex`\n\n### Introduction\n\nThis documentation is for RouteX with [panx-framework](https://panx.eu/). The [panx-framework](https://panx.eu/) will autoload files etc., without that, you need to write own handler. But the usage is the same with or without [panx-framework](https://panx.eu/).\n\nRouteX is default routing system of [panx-framework](https://panx.eu/). The documentation for RouteX is available [here](https://panx.dev/docs/routes) (Some things can be different, because the documentation is written for use with [panx-framework](https://panx.eu/). \n\n### Running RouteX without panx-framework\n\n```php\nrequire 'vendor/autoload.php';\n\nuse AlexKratky\\URL;\nuse AlexKratky\\Route;\nuse AlexKratky\\RouteHandler;\n\nRoute::setError(Route::ERROR_NOT_FOUND, \"404.php\"); // only if you want custom error 404 page\nRoute::set('/', 'home.php'); // set routes - URI: '/', template file: 'home.php'\n\n$URL = new URL(); // Creates a new instance of URL object\nRoute::setMiddlewaresPath($_SERVER['DOCUMENT_ROOT'] . '/middlewares/'); // only if you will use middlewares\nRoute::setControllersPath($_SERVER['DOCUMENT_ROOT'] . '/controllers/'); // optional (faster improvement)\n$template_files = Route::search($URL-\u003egetString()); // Searches in all routes and find the right template file\n\n$rh = new RouteHandler($_SERVER['DOCUMENT_ROOT'] . '/template/', $_SERVER['DOCUMENT_ROOT'] . '/controllers/', $_SERVER['DOCUMENT_ROOT'] . '/handlers/'); // *\n$rh-\u003esetHandlers([\n    'latte' =\u003e 'LatteHandler'\n]); // Sets custom handler for *.latte extension\n$rh-\u003ehandle($template_files); // includes all files or execute function\n```\n*Creates a new instance of RouteHandler\n\n- RouteHandler will require all files provided by Route::search()\n- The first parameter is `template` directory\n- The second parameter is `controllers` directory\n- The third parameter is `handlers` directory\n\n*Handlers are needed for all extension that is not .\\*php file. You can find more about handlers [here]( https://panx.dev/docs/handlers ).*\n\n### Syntax\n\nSyntax of routes:\n\n```php\nRoute::set(\"/URI/TO/HANDLE\", \"File_to_require.php\");\n```\n\nIn URI, you can use wildcards (+, *).\n\nThe `+` meaning is one element, for example: `/post/+/edit` will handle `/post/1/edit`, `/post/2/edit`, ...\n\nThe ` *` meaning is one or more elements, for example: `/post/*` will handle `/post/1`, but also `/post/1/edit`, `/post/2/view`, ...\n\nBut in URI you can use parameters too, for example `/post/{ID}/edit` have same meaning as `/post/+/edit`, but you can access to `{ID}`  using `Route::getValue(\"ID\")`.\n\n### Default routes\n\nIn `/routes/` directory is also file called `default.php` containing default routes. In default routes are routes to error pages. For example Error 404. When you need to set route for error code (403, 404), you need to call function `Route::setError` instead of `Route::set`. Example syntax: \n\n ```php\nRoute::setError(404, \"default/errors/404.php\");\n ```\n\n### Using wildcards\n\nKeep in mind that routes with wildcard should be last, for example:\n\n```php\nRoute::set(\"/\", \"home.php\");\nRoute::set(\"*\", \"test.php\");\n```\n\nThis will display `home.php` file to all users requesting to `/`, and `test.php` to all users requesting something difference. \n\nBut if you write something like this:\n\n```php\nRoute::set(\"*\", \"test.php\");\nRoute::set(\"/\", \"home.php\");\n```\n\nThe result will be different. It will display `test.php` to all users, whatever user request. So if user tries to request `/`, framework will display `test.php` instead of `home.php`. So in this case, the second route is useless, and should be deleted to keep route file clear.\n\n### \u003c action \u003e and \u003c controller \u003e wildcards\nThese wildcards will automatically include controller and call the contoller method specified by action. For example, you will have following route\n```php\n Route::set(\"/admin/\u003ccontroller\u003e/\u003caction\u003e\", \"form.php\");\n```\nand the user will visit with this URI: `/admin/auth/login/`. This will include AuthController and call the contoller's method specified by action - login() (AuthContoller::login()).\n\n### Parameters with regex rule\nYou can limit the route parameter by regular expression. For example, you want to use parameter {ID}, which should be just number, so you can do it by this:\n\n```php\nRoute::set(\"example/{ID[^[0-9]*$]}\", \"id.php\");\n```\n\nIf you enter something different then just ID, it will try to find other route, otherwise display 404.\n\n### Parameters with Validator rule\n\nYou can limit the route parameter by Validator rule. For example, you want to use parameter {NAME}, which should be valid name, so you can do it by this:\n\n```php\nRoute::set(\"/{NAME#Validator::validateUsername}\");\n```\n\n### Including multiple files\n\nIf you want to include more template files, then you need to pass array, for example:\n\n```php\nRoute::set(\"/docs/intro\", [\"header.php\", \"intro.php\", \"footer.php\"]);\n```\n\n### No files\n\nYou can leave second parameter (files or function) empty, so no files will be required, only if you set up the Controller, then will be called.\n\n### Routes with redirect\n\nIf you want to redirect from one route to another, you can do it by this:\n\n```php\nRoute::set(\"/docs\", function() {\n    redirect(\"/docs/intro\");\n});\nRoute::set(\"/docs/intro\", [\"header.php\", \"intro.php\", \"footer.php\"]);\n```\n\n### Routes with function\n\nThe second parameter of route can be anonymous function (Like in redirect).\n\n``````php\nRoute::set(\"/dumpServerVariable\", function() {\n    echo 'This route will dump() $_SERVER variable';\n    dump($_SERVER); // Function from panx\n});\n``````\n\n### Locking route to method\n\nIf you need route accessible only from certain http methods, you can do it by passing third argument.\n\n```php\nRoute::set(\"/\", \"home.php\", [\"GET\"]);\n```\n\nRoute above will be accessible only using GET method.\n\n```php\nRoute::set(\"/postGet\", \"example.php\", [\"POST\", \"GET\"]);\n```\n\nRoute above will be accessible using methods POST and GET.\n\nThe third argument is always an array. If you visit route, that does not support that method you are requesting, you will get Error 400 - Bad request.\n\n\n\n### API routes\n\nAPI routes should be in `/routes/api.php` file. You do not sets the route using function `Route::set()`, but using `Route::apiGroup()`. For example:\n\n```php\nRoute::apiGroup(\"v1\", array(\n    // /api/v1/list\n    array(\"list\", function(){\n        echo \"list\";\n    }),\n    // /api/v1/getlatestversion/stable\n    array(\"getlatestversion/stable\", function() {\n        echo \"0.1\";\n    }, [\"GET\"]),\n));\n```\n\nAs the first parameter, you specify the version of API, for example `v1`, second parameter is array, containing all routes. Each route is single array. In route array, the first parameter is URI, the second parameter is function , file or array of files. The third parameter is optional. If you enter third parameter, it will lock route to certain http methods (See `Locking route to method` for more details).\n\nWhy you should use API routes instead of classic routes? If the first element in URI is `/api/`, it will check all API routes first, after it will check all classic routes. So it will increase the speed of response.\n\n`Route::apiGroup()` function will generate route by following patern: `/api/{VERSION}/{URI}`, where `{VERSION}` is for example `v1`, and `{URI}` is for example `list` or `getlatestversion/stable` as in example above.\n\n### Controllers\n\nYou can setup controller using setController() function:\n\n ```php\nRoute::set('/login', 'auth/login.latte')-\u003esetController(\"AuthController\");\n ```\n\nTo get more info about controller, see [Controllers](https://panx.eu/docs/controllers)\n\n### Middlewares\n\nYou can setup controller using setMiddleware() function:\n\n```php\nRoute::set('/verifymail/{TOKEN}', function() {\n    \n})-\u003esetMiddleware(['AuthMiddleware']);\n```\n\nThe parameter must be always array, even if you set only one middleware.\n\nTo get more info about middlewares, see [Middlewares](https://panx.eu/docs/middlewares)\n\n### API Endpoints\n\nYou can set API endpoint using function Route::setApiEndpoint():\n\n```php\nRoute::setApiEndpoint(\"v3\", new API(\"v3\"));\n```\n\nTo get more info about API endpoints, see [API Endpoints](https://panx.eu/docs/api-endpoints)\n\n### API Extended syntax\n\nBecause API route have many parameters, it is recommended to use following syntax:\n\n```php\nRoute::apiGroup(\"v5\", array(\n    array(\n        \"route\" =\u003e \"test/{name}/{action}\",\n        \"files\" =\u003e null,\n        \"lock\" =\u003e null,\n        \"required_params\" =\u003e null,\n        \"action\" =\u003e \"login\",\n        \"alias\" =\u003e \"test\"\n    ),\n));\n```\n\n\n\n### Aliases\n\nYou can set up alias to each route. This is useful for your template files, because you will not write the absolute routes, e.g. '/login', but you will use the alias. If you using Latte, then you can do it by n:link, e.g. n:link=\"login\" and when you change the route in route file, you do not need to edit the template file. In n:link, you can also include parameters for route and GET method. So if your route have wildcard, you can do it like this:\n\n```php+HTML\n\u003c?php\nRoute::set(\"/test/of/route/\u003caction\u003e/{NAME}/{ID}/*\", function() {echo \"Hello\";});\n?\u003e\n\n\u003ca n:link=\"test, 'action=akce:jmeno:identifikator:hvezdicka=[1,10,20,30]', 'id=10:remember=true:test'\"\u003emacro test\u003c/a\u003e\n\n\u003c!-- which will create --\u003e\n\u003ca href=\"/test/of/route/akce/jmeno/identifikator/1/10/20/30/?id=10\u0026remember=true\u0026test\"\u003emacro test\u003c/a\u003e\n```\n\nTo set alias for API route, you need as 6th parameter ([5]) or by extended syntax.\n\n### Required parameters\n\nYou can set required parameters using -\u003esetRequiredParameters(array $get, array $post);\n\nWhere the $get and $post  are arrays containing the names of the inputs.\n\n\n\n## Other functions of Route class\n\n### Route::searchWithNoLimits()\n\nThis function will return template file(s)/function without limitation of middlewares etc. Works only for API routes. Used in API endpoints \u0026 caching routes.\n\n### Route::convertRoute($route = null)\n\nConvert the URI to route.\n\n- For example, if you set route '/example/+/edit' in route.php and you pass the URI to this function (e.g., /example/13/edit), it will returns the route with wildcard -\u003e '/example/+/edit'\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexkratky%2Froutex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexkratky%2Froutex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexkratky%2Froutex/lists"}