PHP code example of frostealth / yii2-presenter
1. Go to this page and download the library: Download frostealth/yii2-presenter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
frostealth / yii2-presenter example snippets
namespace app \presenters ;
use app \models \User ;
use frostealth \yii2 \presenter \Presenter ;
class UserPresenter extends Presenter
{
public function getFullName ()
{
return implode(' ' , [$this ->firstName, $this ->lastName]);
}
public function getBirthDate ()
{
return date('y.M.d' , $this ->entity->birthDate);
}
public function fields ()
{
$fields = parent ::fields();
$fields[] = 'fullName' ;
return $fields;
}
}
namespace app \models ;
use app \presenters \UserPresenter ;
use frostealth \presenter \interfaces \PresentableInterface ;
use frostealth \yii2 \presenter \traits \PresentableTrait ;
class User extends ActiveRecord implements PresentableInterface
{
use PresentableTrait ;
public function fields ()
{
$fields = parent ::fields();
unset ($fields['passwordHash' ], $fields['passwordResetToken' ]);
return $fields;
}
protected function getPresenterClass ()
{
return 'app\presenters\UserPresenter' ;
}
}
<dl>
<dt>Name</dt>
<dd><? = $model->presenter()->fullName
namespace app \controllers ;
use yii \rest \ActiveController ;
class UserController extends ActiveController
{
public $serializer = 'frostealth\yii2\presenter\rest\Serializer' ;
public $className = 'app\models\User' ;
}