{"id":14966853,"url":"https://github.com/locustv2/yii2-linkable-behavior","last_synced_at":"2025-10-09T05:05:15.272Z","repository":{"id":62518577,"uuid":"86833670","full_name":"Locustv2/yii2-linkable-behavior","owner":"Locustv2","description":"Yii2 behavior to help creating urls easier","archived":false,"fork":false,"pushed_at":"2017-04-07T18:27:01.000Z","size":14,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T05:04:06.259Z","etag":null,"topics":["behavior","linkable","urls","yii2","yii2-behaviors","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Locustv2.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":"2017-03-31T15:25:27.000Z","updated_at":"2023-07-11T14:40:55.000Z","dependencies_parsed_at":"2022-11-02T13:46:04.757Z","dependency_job_id":null,"html_url":"https://github.com/Locustv2/yii2-linkable-behavior","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Locustv2/yii2-linkable-behavior","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Locustv2%2Fyii2-linkable-behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Locustv2%2Fyii2-linkable-behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Locustv2%2Fyii2-linkable-behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Locustv2%2Fyii2-linkable-behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Locustv2","download_url":"https://codeload.github.com/Locustv2/yii2-linkable-behavior/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Locustv2%2Fyii2-linkable-behavior/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000736,"owners_count":26082933,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"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":["behavior","linkable","urls","yii2","yii2-behaviors","yii2-extension"],"created_at":"2024-09-24T13:37:02.910Z","updated_at":"2025-10-09T05:05:15.256Z","avatar_url":"https://github.com/Locustv2.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Linkable Behavior for Yii2 Components\n\nThis extension help creating urls easier in yii2. This behavior provides support for components that have a page to display its contents. The page can be an action in a Module or simply in a Controller. It will be easier to get links related to this record without having to write Url Route over and over again.\n\n[![Latest Stable Version](https://poser.pugx.org/locustv2/yii2-linkable-behavior/v/stable)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)\n[![Total Downloads](https://poser.pugx.org/locustv2/yii2-linkable-behavior/downloads)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)\n[![Latest Unstable Version](https://poser.pugx.org/locustv2/yii2-linkable-behavior/v/unstable)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)\n[![License](https://poser.pugx.org/locustv2/yii2-linkable-behavior/license)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)\n\n\n## Installation\n\nThe preferred way to install the library is through [composer](https://getcomposer.org/download/).\n\nEither run\n```\nphp composer.phar require --prefer-dist locustv2/yii2-linkable-behavior\n```\n\nor add\n```json\n{\n    \"require\": {\n        \"locustv2/yii2-linkable-behavior\": \"~1.0.0\"\n    }\n}\n```\nto your `composer.json` file.\n\n## Usage\n\nAdd the behavior to your ActiveRecord that can he hotlinked:\n\n```php\nnamespace app\\models;\n\nuse yii\\db\\ActiveRecord;\nuse locustv2\\behaviors\\LinkableBehavior;\n\nclass User extends ActiveRecord\n{\n  //...\n\n    public function behaviors()\n    {\n        return ArrayHelper::merge(parent::behaviors(), [\n            [\n                'class' =\u003e LinkableBehavior::className(),\n                'route' =\u003e '/user',\n                'defaultAction' =\u003e 'view',\n                'hotlinkTextAttr' =\u003e 'username',\n                'defaultParams' =\u003e function ($record) {\n                    return [\n                        'id' =\u003e $record-\u003eid,\n                    ];\n                },\n            ]\n        ]);\n    }\n}\n```\n\n```php\nnamespace app\\models;\n\nuse yii\\db\\ActiveRecord;\nuse locustv2\\behaviors\\LinkableBehavior;\n\nclass Photo extends ActiveRecord\n{\n  //...\n\n    public function behaviors()\n    {\n        return ArrayHelper::merge(parent::behaviors(), [\n            [\n                'class' =\u003e LinkableBehavior::className(),\n                'route' =\u003e '/photo',\n                'defaultAction' =\u003e 'view',\n                'linkableParams' =\u003e function ($record) {\n                    return [\n                        'photoid' =\u003e $record-\u003eid,\n                    ];\n                },\n                'useAbsoluteUrl' =\u003e true,\n                'defaultParams' =\u003e function ($record) {\n                    return [\n                        'id' =\u003e $record-\u003eid,\n                        'slug' =\u003e $record-\u003eslug\n                    ];\n                },\n            ]\n        ]);\n    }\n}\n```\n\nWith that code in place, you can now use 4 available methods in your `User` and `Photo` ActiveRecord:\n - `getUrlRoute($action = null, array $params = [])`\n - `getUrlRouteTo(Component $component, $action = null)`\n - `getHotlink($action = null, array $params = [], array $options = [])`\n - `getHotlinkTo(Component $component, $action = null, array $params = [], array $options = [])`\n\n\n### Examples (assuming that you use [pretty urls](http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#using-pretty-urls))\n\n#### `getUrlRoute($action = null, array $params = [])`\n```php\nuse yii\\helpers\\Url;\nuse app\\models\\User;\n\n$user = User::findOne(['id' =\u003e 123]);\n\n// /user/view?id=123\necho Url::to($user-\u003eurlRoute);\n\n// /user/update?id=123\necho Url::to($user-\u003egetUrlRoute('update'));\n\n// http://www.yoursite.com/user/profile?id=123\u0026ref=facebook\necho Url::to($user-\u003egetUrlRoute('profile', ['ref' =\u003e 'facebook']), true);\n```\n\n#### `getUrlRouteTo(Component $component, $action = null)`\n```php\nuse yii\\helpers\\Url;\nuse app\\models\\User;\n\n$user = User::findOne(['id' =\u003e 123]);\n$photo = $user-\u003egetPhotos()-\u003eone();\n\n// /user/photo/view?id=123\u0026photoid=456\u0026slug=my-first-photo\necho Url::to($user-\u003egetUrlRouteTo($photo));\n\n// /photo/user/view?id=456\u0026slug=my-first-photo\u0026uid=123\necho Url::to($photo-\u003egetUrlRouteTo($user));\n\n// /user/photo/update?id=123\u0026photoid=456\u0026slug=my-first-photo\necho Url::to($user-\u003egetUrlRouteTo($photo, 'update'));\n```\n\n#### `getHotlink($action = null, array $params = [], array $options = [])`\n```php\nuse yii\\helpers\\Url;\nuse app\\models\\User;\n\n$user = User::findOne(['id' =\u003e 123]);\n\n// \u003ca href=\"/user/view?id=123\"\u003eLocustv2\u003c/a\u003e\necho $user-\u003ehotLink;\n\n// \u003ca href=\"/user/update?id=123\"\u003eLocustv2\u003c/a\u003e\necho $user-\u003egetHotlink('update');\n\n// \u003ca class=\"text-bold\" href=\"/user/profile?id=123\u0026ref=facebook\"\u003eLocustv2\u003c/a\u003e\necho $user-\u003egetHotlink('profile', ['ref' =\u003e 'facebook'], ['class' =\u003e 'text-bold']);\n```\nIf you want to use absolute urls, you should set `LinkableBehavior::$useAbsoluteUrl` to `true`.\nIf you want to disable hotlinks, you should set `LinkableBehavior::$disableHotlink` to `true`. `\u003cspan/\u003e` will be used instead of `\u003ca/\u003e`\n\n#### `getHotlinkTo(Component $component, $action = null, array $params = [], array $options = [])`\n```php\nuse yii\\helpers\\Url;\nuse app\\models\\User;\n\n$user = User::findOne(['id' =\u003e 123]);\n$photo = $user-\u003egetPhotos()-\u003eone();\n\n// \u003ca href=\"http://www.yoursite.com/user/photo/view?id=123\u0026photoid=456\u0026slug=my-first-photo\"\u003eLocustv2\u003c/a\u003e\necho $user-\u003egetHotlinkTo($photo);\n\n// \u003ca href=\"http://www.yoursite.com/photo/user/view?id=456\u0026slug=my-first-photo\u0026uid=123\"\u003ehttp://www.yoursite.com/photo/user/view?id=456\u0026slug=my-first-photo\u0026uid=123\u003c/a\u003e\necho $photo-\u003egetHotlinkTo($user);\n\n// \u003ca class=\"font-bold\" href=\"http://www.yoursite.com/user/photo/update?id=123\u0026photoid=456\u0026slug=my-first-photo\u0026ref=homepage\"\u003eLocustv2\u003c/a\u003e\necho Url::to($user-\u003egetHotlinkTo($photo, 'update', ['ref' =\u003e homepage], ['class' =\u003e 'font-bold']));\n\n```\n\n\n## To do\n - Add unit tests\n\n\n## Contributing\nFeel free to send pull requests.\n\n\n## License\nFor license information check the [LICENSE](LICENSE.md)-file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocustv2%2Fyii2-linkable-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flocustv2%2Fyii2-linkable-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocustv2%2Fyii2-linkable-behavior/lists"}