{"id":16368913,"url":"https://github.com/apsdehal/link","last_synced_at":"2025-03-15T11:32:48.491Z","repository":{"id":17443350,"uuid":"20216988","full_name":"apsdehal/Link","owner":"apsdehal","description":"A PHP router that helps you create webapps and APIs effortlessly","archived":false,"fork":false,"pushed_at":"2017-04-20T20:39:26.000Z","size":49,"stargazers_count":280,"open_issues_count":1,"forks_count":14,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-10-12T02:54:37.962Z","etag":null,"topics":["api","php","router","webapps"],"latest_commit_sha":null,"homepage":"http://apsdehal.in/Link","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/apsdehal.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":"2014-05-27T11:04:18.000Z","updated_at":"2024-09-17T13:50:44.000Z","dependencies_parsed_at":"2022-09-24T16:51:11.498Z","dependency_job_id":null,"html_url":"https://github.com/apsdehal/Link","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsdehal%2FLink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsdehal%2FLink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsdehal%2FLink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsdehal%2FLink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apsdehal","download_url":"https://codeload.github.com/apsdehal/Link/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221573962,"owners_count":16845942,"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":["api","php","router","webapps"],"created_at":"2024-10-11T02:54:11.201Z","updated_at":"2024-10-26T19:45:21.212Z","avatar_url":"https://github.com/apsdehal.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Link\nA __minimal__ router for your php webapps and APIs that effortlessly links all your project. Its fast and to the point.\n\n# Features\n- RESTful routing\n- Wildcards for your limitless creativity\n- Named routes to help you create links easily\n- Self documented, speaks its own legacy\n- Before and after routes function support\n- Tested with PHP \u003e5.3\n\n## HHVM Version\n\nHHVM version of Link can be found at https://github.com/bmbsqd/Link-Hack . Thanks to [Andy Hawkins](https://github.com/a904guy) for creating it.\n\n\n# Installation\n\n## Composer\n\nFor install from composer just add the following line to your project's composer.json file\n```json\n\t\"require\" : {\n    \t\"link/link\" : \"dev-master\"\n    }\n```\n\nThen run `php composer.phar install`\n\n## Manually\n\nRun `git clone https://github.com/apsdehal/Link.git` in your project's home directory and include it using\n\n```php\n\trequire(\"Link/src/Link.php\");\n```\n\n# Basics\n\n## Simple Routing\n\nRouting is too simple with Link, following example supports it:\n\n```php\n\u003c?php\n\nfunction routeMe(){\n\techo 'I am routed';\n}\n\nLink::all( array(\n\t'/' =\u003e 'routeMe'\n));\n```\n\n## Named Routing\n\nIn Link routes can be named and then further used generatings links in a simple and elegant way.\n\n```php\n\n\u003c?php\n\nfunction nameMe(){\n\techo 'I am named';\n}\n\nLink::all( array(\n\t'/named' =\u003e ['nameMe', 'Its my name']\n));\n```\n\nNames to routes must be given as second argument in array while the first being the route handler.\n\n### Usage\n\nThese named routes can be used in creating in hassle free links.\n\n```html\n\t\u003ca href=\"\u003c?php echo Link::route('Its my name') ?\u003e\"\u003eGo to named route\u003c/a\u003e\n```\n\n## Routing with classes\n\nLink can handle classes as Route handler easily, but remember non-static class will be handled RESTfully.\n\n```php\n\n\u003c?php\n\n$routes = array(\n\t'/' =\u003e 'IndexController::getMeHome', //Static function\n    '/home' =\u003e 'HomeController', //RESTful class\n    '/office' =\u003e 'OfficeController'\n);\n\nLink::all($routes)\n```\n\n## RESTful routing\n\nRESTful routing is a breeze for Link.\n\n```php\n\n\u003c?php\n\nclass HomeController\n{\n\t\n    function get(){\n    \techo 'You have got to home :)';\n    }\n    \n    function post(){\n    \techo 'You have posted to home';\n    }\n    \n    function put(){\n    \techo 'You have put to home';\n    }\n    \n    function delete(){\n    \techo 'You have deleted the home :(';\n    }\n}\n\nLink::all( array (\n\t'/' =\u003e ['HomeController', 'HomeRoute']\n));\n```\n## WildCards\n\nLink supports numbers, string and alphanumeric wildcards which can be used as `{i} {s} {a}` respectively and of course it can render regex also. Example will clear away your doubts\n\n```php\n$routes = array(\n\t'/' =\u003e 'IndexController',\n    '/{i}' =\u003e 'IndexController',\n    //Parameter in place of {i} will be passed to IndexController\n\t'/posts/{a}/{i}/{s}' =\u003e 'PostsController'\n);\n\nLink::all($routes);\n```\n## Supplimentary Handlers\n\n### Universal Extra Handlers\n\nThrough Link, universal before and after handlers can be added, such that these are executed always before any route is routed. This can be done as follows:\n\n```php\n\u003c?php\nfunction universalBeforeHandler( $id ) {\n    echo 'Hello I occured before with ' . $id . '\\n';\n}\n\nfunction universalAfterHandler( $id ) {\n    if( $id )\n        echo 'Hello I occured after with ' . $id;\n    else\n        echo 'I simply occured after';\n}\n\nfunction main(){\n    echo 'I simply occured\\n'\n}\n\nLink::before( 'universalBeforeHandler', ['12'] ); //If you want to pass parameters to them, pass them as arrays\nLink::before( 'universalBeforeHandler'); //else don't even pass them\n\nLink::all( array(\n    '/' =\u003e 'main'\n    ))\n```\n\nNow go to '/' in your browser to find:\n\nHello I occured before with 12\n\nI simply occured\n\nI simply occured after.\n\n### Single Route\n\nYou can add a before (middle) handler to a specific route, just pass the before handler to routes array as third parameters. The wildcards extracted from route will be passed to to before handler and if it return some array, this array will be passed further to main handler but if not the original extracted wildcards would be passed away. Make sure you return an array from before handler.\n\n```php\n\u003c?php \nfunction beforeHandler( $name ) {\n    return [ $name . ' Link' ];\n}\n\nfunction mainHandler( $name ){\n    echo $name;\n}\n\nLink::all(array(\n    '/{s}' =\u003e ['mainHandler', 'Main', 'beforHandler']\n    ));\n``` \n\nGo to '/aps' in browser, you will get *aps Link*.\n\n## Passing Parameters to Named Routes\n\nYou can pass parameters to named routes if the have wildcards in the route path, this will thus generate dynamic links through a single named route.\n\n```php\n\n\u003c?php\n\nfunction nameMe( $i, $s ){\n\techo 'I am named and I have been passed ' . $i . $s ;\n}\n\nLink::all( array(\n\t'/named/{i}/{s}' =\u003e ['nameMe', 'Its my name']\n));\n```\n\nNow generate a link through Link\n\n```php\n\necho Link::route( 'Its my name', array(1, 'Me') );\n```\n\nThis in turn will generate `YOUR_DOMAIN/named/1/Me`.\n\n## 404 Errors\n\nYou should probably add a 404 handler to your routes array, rest Link will take care of handling routes that are not found. In case, Link doesn't find a 404 route defined, it will just send a 404 header.\n\n# Server Configuration\n\n## Apache\n\nYou should add the following code snippet in your Apache HTTP server VHost configuration or **.htaccess** file.\n\n```apacheconf\n\u003cIfModule mod_rewrite.c\u003e\n    RewriteEngine on\n    RewriteCond %{REQUEST_FILENAME} !-f\n    RewriteCond %{REQUEST_FILENAME} !-d\n    RewriteCond $1 !^(index\\.php)\n    RewriteRule ^(.*)$ index.php/$1 [L]\n\u003c/IfModule\u003e\n```\n\nAlternatively, in a version of Apache greater than 2.2.15, then you can use this:\n```apacheconf\nFallbackResource /index.php\n```\n\n# Notes\n\nIf you are planning to use non-Restful method and non-static classes, then use them as follows:\n\n```php\nclass HelloHandler \n{\n    public function home(){\n        echo 'Hello home';\n    }\n}\n\n$helloObject = new HelloHandler();\n\nLink::all( array(\n    '/' =\u003e array( $helloObejct, 'home' )\n    ))\n```\n\nSo you need to pass such functions as `array( $object, 'functionName' )`\n\n# Contributions\n\nThanks to all people below for contributing to Link.\n\n- @jedmiry\n- @pborelli\n\n# License\n\nLink is available under MIT license, so feel free to contribute and modify it as you like. Free software, Yeah!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapsdehal%2Flink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapsdehal%2Flink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapsdehal%2Flink/lists"}