YII Micro-Frame

Source: Internet
Author: User
Tags composer install

Directory

    • Installation
    • Configuration
    • First API
      • API style
      • return some data
    • Other

There are times when it is not necessary to use the full Yii framework, and YII itself is a micro-framework.

Here are the official: Yii 2.0 authoritative Guide-using Yii as a micro-frame

Installation

This section is no different from the official Yii documentation.

  • Create a new working folder, which is namedmicro-app

  • Create a new file under this folder composer.json :

    {    "require": {        "yiisoft/yii2": "~2.0.0"    },    "repositories": [        {            "type": "composer",            "url": "https://packagist.laravel-china.org"        }    ]}

    Please note here that the url Composer Official library is https://asset-packagist.org , because of the wall reason, the domestic access speed is relatively slow, therefore replaced the above Laravel-china to build the library.

  • Run composer install command (under Windows composer.bat install )

  • Constructs the project structure, under which the new Web , controllers , models folders are created, similar to the Yii base version. The Web folder is used as an accessible directory, inside the portal file index.php :

      <?php//comment out the Following lines when deployed to productiondefined (' yii_debug ') or define (' Yii_debug ', true);d efined (' yii_env ') or de Fine (' yii_env ', ' dev '); require (__dir__. ‘/.. /vendor/autoload.php '); require (__dir__. ‘/.. /vendor/yiisoft/yii2/yii.php '); $config = Require __dir__. ‘/.. /config.php ';(new Yii\web\application ($config))->run ();  
  • Create a new profile and micro-app create a new file in the directory config.php :

    <?phpreturn [    ‘id‘ => ‘micro-app‘,    // the basePath of the application will be the `micro-app` directory    ‘basePath‘ => __DIR__,    // this is where the application will find all controllers    ‘controllerNamespace‘ => ‘micro\controllers‘,    // set an alias to enable autoloading of classes from the ‘micro‘ namespace    ‘aliases‘ => [        ‘@micro‘ => __DIR__,    ],];

So you have the following file structure:

micro-app/├── composer.json├── config.php├── web/    └── index.php├── models/└── controllers/
Configuration

config.phpWrite configuration information in (can refer to Yii Basic or Premium version, is the same):

    ‘components‘ => [        // 数据库:        // 使用mysql数据库,数据库地址是localhost,使用的库是cms        // 数据库用户名:root,密码:123456        ‘db‘ => [            ‘class‘ => ‘yii\db\Connection‘,            ‘dsn‘ => ‘mysql:host=localhost;dbname=cms‘,            ‘username‘ => ‘root‘,            ‘password‘ => ‘123456‘,        ],        // 请求:        // 允许使用JSON格式的请求,并配置JSON解析器        ‘request‘ => [            ‘parsers‘ => [                ‘application/json‘ => ‘yii\web\JsonParser‘,            ]        ],        // 响应:        // 返回数据的格式为JSON        ‘response‘ => [            ‘format‘ => ‘json‘,        ],        // URL路由:        // 参考官方文档可以对URL进行美化、语义化//        ‘urlManager‘ => [//            ‘enablePrettyUrl‘ => true,//            ‘showScriptName‘ => false,//            ‘enableStrictParsing‘ => true,//            ‘rules‘ => [//            ],//        ],    ],
The first style of APIAPI

The API here is a non-restful style, but a custom format:

{    ‘Status‘: 200,    ‘Data‘: [],    ‘ErrorMessage‘: ‘‘}

Status: Business status Code, reference HTTP status code, 200 indicates success

Data: Business Data

ErrorMessage: Error message, empty if error-free

The party receiving the API data, when the response is received, the first judgment Status , if the error should be based on ErrorMessage appropriate processing.

return some data

1. models Create a new file under the folder User.php :

<?phpnamespace micro\models;use yii\db\ActiveRecord;class User extends ActiveRecord{}

2. controllers create a new file under the folder SiteController.php :

<?phpnamespace micro\controllers;use micro\models\User;use yii\web\Controller;class SiteController extends Controller{    public function actionIndex()    {        return [            ‘Status‘ => 200,            ‘Data‘ => User::findOne([‘id‘ => 1]),            ‘ErrorMessage‘ => ‘‘,        ];    }}

3. Visit

    • If you have web set the folder to access the directory, you can try to access:

      http://localhost/?r=site/index

    • If your Access directory is micro-app , then try to access:

      http://localhost/web/?r=site/index

The response is presumably:

Other
    • URLs can do some landscaping, refer to: YII 2.0 authoritative Guide-routing
    • Receive data can still be used as in the Yii basic version, such as the use Yii::$app->request->post() of
    • You can \web\index.php set up a test environment or a production environment in, as with the Yii Base edition

YII Micro-Frame

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.