{"id":15282379,"url":"https://github.com/yii2tech/behavior-trait","last_synced_at":"2025-10-06T23:30:32.680Z","repository":{"id":34487137,"uuid":"38426711","full_name":"yii2tech/behavior-trait","owner":"yii2tech","description":"Allows handling events via inline declared methods, which can be added by traits","archived":true,"fork":false,"pushed_at":"2019-07-03T11:39:31.000Z","size":9,"stargazers_count":20,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-03-26T00:01:16.453Z","etag":null,"topics":["behavior","trait","yii","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yii2tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["klimov-paul"],"patreon":"klimov_paul"}},"created_at":"2015-07-02T10:32:46.000Z","updated_at":"2024-03-26T00:01:16.453Z","dependencies_parsed_at":"2022-09-14T10:11:08.712Z","dependency_job_id":null,"html_url":"https://github.com/yii2tech/behavior-trait","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fbehavior-trait","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fbehavior-trait/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fbehavior-trait/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fbehavior-trait/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2tech","download_url":"https://codeload.github.com/yii2tech/behavior-trait/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219877454,"owners_count":16554878,"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":["behavior","trait","yii","yii2","yii2-extension"],"created_at":"2024-09-30T14:25:26.579Z","updated_at":"2025-10-06T23:30:32.329Z","avatar_url":"https://github.com/yii2tech.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yii2tech\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars2.githubusercontent.com/u/12951949\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eBehavior Trait Extension for Yii 2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThis extension provides the ability of handling events via inline declared methods, which can be\nadded via traits.\n\nFor license information check the [LICENSE](LICENSE.md)-file.\n\n[![Latest Stable Version](https://poser.pugx.org/yii2tech/behavior-trait/v/stable.png)](https://packagist.org/packages/yii2tech/behavior-trait)\n[![Total Downloads](https://poser.pugx.org/yii2tech/behavior-trait/downloads.png)](https://packagist.org/packages/yii2tech/behavior-trait)\n[![Build Status](https://travis-ci.org/yii2tech/behavior-trait.svg?branch=master)](https://travis-ci.org/yii2tech/behavior-trait)\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2tech/behavior-trait\n```\n\nor add\n\n```json\n\"yii2tech/behavior-trait\": \"*\"\n```\n\nto the require section of your composer.json.\n\n\nUsage\n-----\n\nThis extension introduces special trait [[\\yii2tech\\behaviortrait\\BehaviorTrait]], which if used provides\nthe ability of handling events via inline declared methods, which can be added via other traits.\nThis trait can be added to any descendant of [[\\yii\\base\\Component]].\n\nEach event handler method should be named by pattern: '{eventName}Handler{UniqueSuffix}', where 'eventName' is a\nname of the event the method should handle, 'UniqueSuffix' any suffix, which separate particular event handler\nmethod from the others.\nFor example: if the class has an event 'beforeInsert' it can introduce method named `beforeInsertHandlerEncryptPassword`,\nwhich will be automatically triggered when event 'beforeInsert' is triggered:\n\n```php\nuse yii\\db\\ActiveRecord;\nuse yii2tech\\behaviortrait\\BehaviorTrait;\n\nclass User extends ActiveRecord\n{\n    use BehaviorTrait; // add `BehaviorTrait` allowing to use inline event handlers\n    use EncryptPasswordTrait; // add trait, which introduce inline event handler\n\n    // ...\n}\n\ntrait EncryptPasswordTrait\n{\n    public function beforeInsertHandlerEncryptPassword(Event $event)\n    {\n        if (!empty($this-\u003enewPassword)) {\n            $this-\u003epassword = sha1($this-\u003enewPassword);\n        }\n    }\n}\n```\n\n\u003e Attention: watch for the naming collisions, ensure any inline handler declared either in class or via trait has\n  a unique name (with unique suffix)!\n\n\u003e Note: using traits instead behaviors improves performance but is less flexible. Keep in mind that such approach\n  has been rejected at Yii2 core at [yiisoft/yii2#1041](https://github.com/yiisoft/yii2/pull/1041).\n","funding_links":["https://github.com/sponsors/klimov-paul","https://patreon.com/klimov_paul"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fbehavior-trait","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2tech%2Fbehavior-trait","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fbehavior-trait/lists"}