Yii2 adding logical layers

Source: Internet
Author: User

Now the framework is basically MVC pattern, such as thinkphp,ci,yii framework and so on

Because the project is bigger and the logic is complex, the simple MVC pattern can not meet the demand, there are a few models in the constant expansion of the model, which produced a network of interdependence. Maintenance is more troublesome.

So we need to decouple this or the expanded model.

So we're thinking about adding a logical layer of logic.

First of all, we have an already installed Yii project. No installation can refer to the YII2 Advanced template installation I wrote earlier

After the installation is complete

There are several main categories:

Backend

Common

Console

Frontend

Vedor

We have added a folder logic under the Comom directory to form a directory structure such as common\logic\.

We're going to let him form such a call. Controller->logic->model,controller no longer calls model directly, and logic calls model.

Suppose we have a articlecontroller.php, a articlelogic.php, a model article.php

Name implies.

Controller Layer articlecontroller.php

1<?PHP2 namespace Frontend\controllers;3 4  UseYii;5  Useyii\base\invalidparamexception;6  Useyii\web\badrequesthttpexception;7  UseYii\web\controller;8 9  Usecommon\logic\articlelogic;Ten  One  A /** - * Article Controller -  */ the classArticlecontrollerextendsController - { -     /** - * http://yii_advanced_app_2_0_9.me/index.php?r=article/get_article_info&article_id=30 + * Get information -      */ +      Public functionActionget_article_info () A     { at         $condition[' ID ']=yii::$app->request->get (' article_id '); -          -         $article _logic=New\common\logic\articlelogic (); -         $get _article_info_result=$article _logic->get_article_info ($condition); -          -         Print_r($get _article_info_result); die(); in          -     } to}

Logic Layer articlelogic.php

<?PHP/** * Article * @author yoper [email protected] * http://www.linglingtang.com **/namespace Common\logic; UseYii;/** * Article Logic*/classArticlelogicextendsyoxbaselogic{Private $article _model=NULL;  Public function__construct () {Parent::__construct (); $this->article_model=New\common\models\article (); }    /** * Get article Information * @param array $condition * @return Multitype:number unknown*/     Public functionGet_article_info ($condition)    {        $result=Array(' Status ' =>0); $get _article_info_result=$this->article_model->get_article_info ($condition); if($get _article_info_result[' Status ']<1)        {            $result[' Message ']=$get _article_info_result[' Message ']; return $result; }        $result[' Status ']=1; $result[' Data ']=$get _article_info_result[' Data ']; return $result; }}

Model Layer article.php

<?PHP/** * Article * @author yoper [email protected] * http://www.linglingtang.com **/namespace Common\models; UseYii; Useyii\base\notsupportedexception; UseYii\behaviors\timestampbehavior; UseYii\db\activerecord;/** * Article model **/classArticleextendsactiverecord{/** * @inheritdoc*/     Public Static functionTableName () {return' {{%article}} '; }    /** * @inheritdoc*/     Public functionbehaviors () {return[Timestampbehavior:: ClassName (),        ]; }    /** * @inheritdoc*/     Public functionrules () {return [            [' Status ', ' Default ', ' value ' = self::status_active],            [' Status ', ' in ', ' range ' = [self::status_active, self::status_deleted]],        ]; }     Public functionGet_article_info ($condition)     {        $result=Array(' Status ' =>0); $info=$this->find ()->where ($condition)->one ()attributes; $result[' Status ']=1; $result[' Message ']= ' get success '; $result[' Data ']=$info; return $result; }}

In this way, we can successfully invoke the data.

, simple printing, ugly, but has the connotation ~

Adding logical layers is done.

Yoper

2016.10.06

00 sugar

Yii2 adding logical layers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.