Answer the question
In order to leave comments, you need to log in
And how do you get rid of duplication in solving typical problems?
Good day.
Task: make catalogs of movies, games and celebrities in Yii2, they all have a lot in common (listing, detailed output, filtering...).
Solution: write a basic Catalog module and make the Films, Games, Stars modules based on it...
For convenience, we will omit the implementation details. This module implements basic functionality that is easy to customize and modify using overrides.
How do you solve such problems? Don't write the same thing every time. For example, Bitrix made infoblocks, a fairly convenient and flexible solution
Example of a solution
/** Описание интерфейса базового модуля Каталог */
abstract class CatalogInterface
{
/** @var bool Отображать фильтр */
public $filter = false;
abstract getModel();
public function getListWidget()
{
return CatalogListWidget::classname();
}
public function getDetailWidget()
{
return CatalogDetailWidget::classname();
}
public function getFilterWidget()
{
return CatalogFilterWidget::classname();
}
}
/** Базовый контроллер каталога */
abstract class CatalogController extends Controller
{
/** @var CatalogInterface Класс описывающий интерфейс модуля */
protected $interface = null;
public function init()
{
$this->interface = Module::getInterface('catalog');
}
public function listAction()
{
if ($this->interface->filter)
{
$filterWidget = $this->interface->getFilterWidget();
$filterWidget::widget();
}
$listWidget = $this->interface->getListWidget();
$listWidget::widget();
}
public function detailsAction()
{
$detailsWidget = $this->interface->getDetailsWidget();
$detailsWidget::widget();
}
}
/** Конфигурация каталога в модуле Фильмы */
class FilmsCatalogInterface extends CatalogInterface
{
/** @var bool Отображать фильтр */
public $filter = true;
}
/** Контроллер каталога в модуле Фильмы */
class FilmsController extends CatalogController
{
public function getModel()
{
return Film::classname();
}
}
Answer the question
In order to leave comments, you need to log in
Solution: write a basic Catalog module and make Films, Games, Stars... modules based on it.
Well, if they are exactly the same .. well, inherit from the model .. in which everything is written and, if necessary, override the methods .. but for the most part, output, saving and reading are still slightly different ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question