Presenter

Presenter is a very simple class that overloads methods and variables so that you can add extra logic to your objects without adding view logic to areas like your models or controllers and also keeps any extra logic out of your views.

How it work in Parus. Let's say you need to show created date for post but not in the same form as it is stored in the database (2016-11-08 19:21:26) but something more readable for example 08 Nov. 2016

  1. Create class (for example in the directory presenters with name PostPresenter) extend BasePresenter.
  2. Create method created_at (do transformations).
  3. By using api specify presenter for post - set_post_presenter('app\presenters\PostPresenter')

This is all, now all methods from a class PostPresenter will be available in all post object.


namespace app\presenters;

use rokorolov\parus\admin\base\BasePresenter;
use Yii;

class PostPresenter extends BasePresenter
{
    public function created_at()
    {
        return Yii::$app->formatter->asDate($this->created_at, 'medium')
    }
}
    
$post = get_post_by('id', 5);
echo $post->created_at; (08 Nov. 2016)

// Now you have good separation of logic and an easy little class you can modify to add properties

Very useful for gallery images that each time not to write paths, just Html::img($image->image_thumb_src). Create class PhotoPresenter with method image_thumb_src and specify presenter for all photo objects set_photo_presenter('app\presenters\PhotoPresenter').


// Available presenters.
set_post_presenter('..')
set_category_presenter('..')
set_album_presenter('..')
set_photo_presenter('..')
set_page_presenter('..')
set_user_presenter('..')
set_language_presenter('..')
set_menu_type_presenter('..')
set_menu_presenter('..')