{"id":15282375,"url":"https://github.com/frostealth/yii2-presenter","last_synced_at":"2026-03-12T14:44:12.870Z","repository":{"id":62507711,"uuid":"47888844","full_name":"frostealth/yii2-presenter","owner":"frostealth","description":"Yii2 View Presenter","archived":false,"fork":false,"pushed_at":"2017-02-09T12:56:02.000Z","size":10,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T22:55:36.412Z","etag":null,"topics":["entity","presenter","view","yii","yii2","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frostealth.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":"2015-12-12T18:06:51.000Z","updated_at":"2020-11-14T16:28:01.000Z","dependencies_parsed_at":"2022-11-02T12:31:49.010Z","dependency_job_id":null,"html_url":"https://github.com/frostealth/yii2-presenter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/frostealth/yii2-presenter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-presenter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-presenter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-presenter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-presenter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frostealth","download_url":"https://codeload.github.com/frostealth/yii2-presenter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-presenter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30428710,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T14:34:45.044Z","status":"ssl_error","status_checked_at":"2026-03-12T14:09:33.793Z","response_time":114,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["entity","presenter","view","yii","yii2","yii2-extension"],"created_at":"2024-09-30T14:25:25.423Z","updated_at":"2026-03-12T14:44:12.853Z","avatar_url":"https://github.com/frostealth.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Yii2 View Presenters\n=============\n\nSo you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) \nis displayed from the view.\n\n* Should that logic be hard-coded into the view? **No.**\n* Should we instead store the logic in the model? **No again!**\n\nInstead, leverage view presenters. That's what they're for! This package provides one such implementation.\n\n## Installation\n\nRun the [Composer](http://getcomposer.org/download/) command to install the latest stable version:\n\n```bash\ncomposer require frostealth/yii2-presenter @stable\n```\n\n## Usage\n\nThe first step is to store your presenters somewhere - anywhere. \nThese will be simple objects that do nothing more than format data, as required.\n\nHere's an example of a presenter.\n\n```php\nnamespace app\\presenters;\n\nuse app\\models\\User;\nuse frostealth\\yii2\\presenter\\Presenter;\n\n/**\n * Class ConcreteEntityPresenter\n *\n * @property User   $entity\n *\n * @property-read string $firstName\n * @property-read string $lastName\n * @property-read string $fullName\n * @property-read string $birthDate\n */\nclass UserPresenter extends Presenter\n{\n    /**\n     * @return string\n     */\n    public function getFullName()\n    {\n        return implode(' ', [$this-\u003efirstName, $this-\u003elastName]);\n    }\n    \n    /**\n     * @return string\n     */\n    public function getBirthDate()\n    {\n        return date('y.M.d', $this-\u003eentity-\u003ebirthDate);\n    }\n    \n    /**\n     * @inheritdoc\n     * @see \\yii\\base\\Arrayable::fields()\n     * @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields\n     */\n    public function fields()\n    {\n        $fields = parent::fields();\n        $fields[] = 'fullName';\n        \n        return $fields;\n    }\n}\n```\n\nNext, on your entity, pull in the `frostealth\\yii2\\presenter\\traits\\PresentableTrait` trait, \nwhich will automatically instantiate your presenter class.\n\nHere's an example of an presentable model.\n\n```php\nnamespace app\\models;\n\nuse app\\presenters\\UserPresenter;\nuse frostealth\\presenter\\interfaces\\PresentableInterface;\nuse frostealth\\yii2\\presenter\\traits\\PresentableTrait;\n\n/**\n * Class User\n *\n * @property string $firstName\n * @property string $lastName\n * @property string $birthDate\n * @property string $passwordHash\n * @property string $passwordResetToken\n *\n * @method UserPresenter presenter()\n */\nclass User extends ActiveRecord implements PresentableInterface\n{\n    use PresentableTrait;\n    \n    /**\n     * @inheritdoc\n     * @see \\yii\\base\\Arrayable::fields()\n     * @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields\n     */\n    public function fields()\n    {\n        $fields = parent::fields();\n        unset($fields['passwordHash'], $fields['passwordResetToken']);\n        \n        return $fields;\n    }\n    \n    /**\n     * @return string|array\n     */\n    protected function getPresenterClass()\n    {\n        return 'app\\presenters\\UserPresenter';\n    }\n}\n```\n\nNow, within your view, you can do:\n\n```php\n\u003cdl\u003e\n    \u003cdt\u003eName\u003c/dt\u003e\n    \u003cdd\u003e\u003c?= $model-\u003epresenter()-\u003efullName ?\u003e\u003c/dd\u003e\n    \n    \u003cdt\u003eBirth Date\u003c/dt\u003e\n    \u003cdd\u003e\u003c?= $model-\u003epresenter()-\u003ebirthDate ?\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n```\n\n### Yii2 REST\n\nHere's an example of an controller.\n\n```php\nnamespace app\\controllers;\n\nuse yii\\rest\\ActiveController;\n\nclass UserController extends ActiveController\n{\n    /** @inheritdoc */\n    public $serializer = 'frostealth\\yii2\\presenter\\rest\\Serializer';\n    \n    /** @inheritdoc */\n    public $className = 'app\\models\\User';\n}\n```\n\n## License\n\nThe MIT License (MIT).\nSee [LICENSE.md](https://github.com/frostealth/yii2-presenter/blob/master/LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostealth%2Fyii2-presenter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrostealth%2Fyii2-presenter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostealth%2Fyii2-presenter/lists"}