⇐ Back to Yii2 Overview | Yii2 App Templates
YiiPowered is a showcase of websites and projects built with Yii framework.
YiiPowered is not really an application template. But it is a non-trivial application, which is available with code at GitHub.
docker build -t yiipowered .
.docker-compose up
.hosts
file (on Windows: Open file C:\Windows\System32\Drivers\etc\hosts
with Notepad running as admin)..php-orig
sample configs in config
directory. Copy all these files to .php
without -orig
and adjust to your needs.docker exec -it yiipowered bash
.composer install && php yii migrate
to setup the database tables.yii user/assign USERNAME admin
to assign admin role to user USERNAME.
The Project
model has these behaviors:
There are queues for
There is a nice example of an attribute evalidation using new and old attribute values:
public function rules() { return [ // ... ['status', 'validateStatus'] ]; } public function validateStatus() { if ($this->isAttributeChanged('status', false)) { if ($this->status == self::STATUS_DELETED && $this->getOldAttribute('status') != self::STATUS_DRAFT) { $this->addError('status', Yii::t('project', 'You can only delete a project from a draft.')); } if ($this->getOldAttribute('status') == self::STATUS_DELETED) { $this->addError('status', Yii::t('project', 'You can not restore a deleted project.')); } } }
'modules' => [ 'api1' => [ 'class' => \app\modules\api1\Module::class, ], ],
Linkable
or fields()
n:m
relation:/** * @return \yii\db\ActiveQuery */ public function getUsers() { return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('{{%project_user}}', ['project_id' => 'id']); }
getCurrentUser
index
action creates and validates a search model, then just returns a DataProvider:class ProjectController extends Controller { public function actionIndex() { $projectSearch = new ProjectSearch(); $projectSearch->load(\Yii::$app->request->get()); if (!$projectSearch->validate()) { throw new BadRequestHttpException('Invalid parameters: ' . json_encode($projectSearch->getErrors())); } return $projectSearch->getDataProvider(); } }
The RSS feed is a regular controller action.
Project
records:$projects = Project::find() ->with('images', 'users') ->limit(50) ->all();
Item
$feed = new Feed(); $feed->title = 'YiiPowered'; $feed->setWebMaster('sam@rmcreative.ru', 'Alexander Makarov'); // ... foreach ($projects as $project) { $url = Url::to(['project/view', 'id' => $project->id, 'slug' => $project->slug], true); $item = new Item(); $item->title = $project->title; // ... $item->setAuthor('noreply@yiipowered.com', implode(', ', $authors)); $feed->addItem($item); } $feed->render();
The api
docs page is a rendered Markdown page.
use yii\helpers\Markdown; // ... class DocsController extends Controller { public function actionIndex() { $docs = file_get_contents(\Yii::getAlias('@app/modules/api1/docs.md')); return $this->render('index', [ 'content' => Markdown::process($docs, 'gfm'), ]); } }
placeholder
attribute→label(false)
:<?= $form->field($model, 'title') ->textInput([/* ... */ 'placeholder' => $model->getAttributeLabel('title')]) ->label(false) ?>
class User extends ActiveRecord implements IdentityInterface { const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; // ... public function rules() { return [ ['status', 'default', 'value' => self::STATUS_ACTIVE], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], ['status', 'filter', 'filter' => 'intval'], // ... ]; } // Get the label for the current status public function getStatusLabel() { $statuses = self::getStatuses(); return ArrayHelper::getValue($statuses, $this->status); } // Get an array of status labels, indexed by status constants public static function getStatuses() { return [ self::STATUS_DELETED => Yii::t('user', 'Delete'), self::STATUS_ACTIVE => Yii::t('user', 'Active'), ]; } }