Generate models in common
when shared between frontend
and backend
. Eg: common/models/Content.php
:
<?php namespace common\models; // <<-- Namespace for common (shared) models for frontend and backend use Yii; ... class Content extends \yii\db\ActiveRecord { ... }
When creating a controller for frontend sharing common models, use this template (eg: frontend/controllers/ContentController.php
:
<?php namespace frontend\controllers; // <<-- Frontend controller use Yii; // For Yii application use common\models\Content; // <<-- Common model use yii\web\Controller; // For base Controller class use yii\web\NotFoundHttpException; use yii\filters\AccessControl; use yii\filters\VerbFilter; class ContentController extends \yii\web\Controller { public function actionIndex() { $model = new Content(); if ($model->load(Yii::$app->request->post())) { if ($model->validate()) { // form inputs are valid, do something here return; } } return $this->render('index', [ 'model' => $model, ]); } }
Create view for frontend. Eg: frontend/views/content/index.php
:
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model common\models\Content */ /* @var $form ActiveForm */ ?> <div class="content-index"> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'title') ?> <?= $form->field($model, 'category_id') ?> <?= $form->field($model, 'hits') ?> <?= $form->field($model, 'rating_sum') ?> <?= $form->field($model, 'rating_count') ?> <?= $form->field($model, 'show_title') ?> <?= $form->field($model, 'show_intro') ?> <?= $form->field($model, 'show_image') ?> <?= $form->field($model, 'show_hits') ?> <?= $form->field($model, 'show_rating') ?> <?= $form->field($model, 'content_type_id') ?> <?= $form->field($model, 'featured') ?> <?= $form->field($model, 'ordering') ?> <?= $form->field($model, 'status') ?> <?= $form->field($model, 'created_by') ?> <?= $form->field($model, 'updated_by') ?> <?= $form->field($model, 'introtext') ?> <?= $form->field($model, 'fulltext') ?> <?= $form->field($model, 'publish_up') ?> <?= $form->field($model, 'publish_down') ?> <?= $form->field($model, 'created_at') ?> <?= $form->field($model, 'updated_at') ?> <?= $form->field($model, 'tags') ?> <?= $form->field($model, 'intro_image') ?> <?= $form->field($model, 'intro_image_float') ?> <?= $form->field($model, 'main_image') ?> <?= $form->field($model, 'main_image_float') ?> <div class="form-group"> <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div><!-- content-index -->
To create links from the backend application to the frontend application, and vice versa, create separate URL manager rules by naming it differently:
Eg: file backend/config/main.php
return [ 'components' => [ 'urlManager' => [ // Modify current Yii::$app->urlManager. // Here is your normal backend url manager config. 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [] ], 'urlManagerFrontend' => [ // Create a new urlManager to point to frontend (Yii::$app->urlManagerFrontend). // Here is your frontend URL manager config. 'class' => 'yii\web\UrlManager', 'baseUrl' => 'a/frontend/web', 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [] ], ], ];
rewrite_module
turned on..htaccess
file.
Create a .htaccess
file in each web
folders (/frontend/web
and /backend/web
) to contain this:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
After this, you can get an URL pointing. Eg: In a backend view, in order to link to frontend, use this:
// Some route on frontend. echo Yii::$app->urlManagerFrontend->createUrl('customer/index'); // OR frontend baseUrl: echo Yii::$app->urlManagerFrontend->createUrl('');
Add a $frontendUrl
global variable to basic/config/params.php
or common/config/params.php
:
Simple add a key and value pairs:
return [ ... 'frontendUrl' => 'http://www.example.com/frontend/web', ];
You can then use that variable anywhere in the system:
<?= Yii::$app->params['frontendUrl'] ?>