Home > Backend Development > PHP Tutorial > Yii multi-application multi-module

Yii multi-application multi-module

不言
Release: 2023-03-22 18:10:02
Original
2387 people have browsed it

This article takes YII 2.0.7 as an example to share with you about Yii multi-application and multi-module. Friends in need can refer to it

Overview

First look at multi-application and multi-module Features of the module:

Features of multiple applications:

  • Independent configuration file

  • Independent domain name

Features of multiple modules:

  • Unified configuration file

  • Unified domain name

So, how to actually decide whether to use multiple applications or multiple modules?

  • For the separation of front and backend, for example, the backend needs a separate domain name for management. This should use multiple applications.

  • The configuration of multiple applications is completely different. It is more convenient to use multiple applications. Use different configuration files

  • Multiple applications require more domain name configurations, which makes price comparison troublesome. For small projects, domain names are not distinguished. Multiple modules are better

Multiple applications

The easiest way is to download the advanced application template of Yii2 from the official website: yii-advanced-app-2.0.12.tgz. After downloading and decompressing, enter the advanced directory and run:

# Windows
init.bat

# Linux
init
Copy after login

will open the web# of the two applications frontend and backend ##Directory generation entry file index.php. frontend and backend represent frontend and background applications respectively. The directory structure inside is the same:

assets/  
config/  
controllers/  
models/  
runtime/  
views/  
web/
Copy after login

Run:

$ cd advanced/frontend/web
$ php -S 0.0.0.0:8888
PHP 5.6.22 Development Server started at Sun Aug 20 21:10:28 2017
Listening on http://0.0.0.0:8888
Copy after login

Open the browser and enter http://0.0.0.0:8888 can access the default home page.

It is recommended that the model is placed in

common/models in the root directory.

Multiple modules

For multiple modules, please refer to

http://www.yiichina.com/doc/g.... Example: Create a new h5 application in frontend:

1. Create related directories

$ cd frontend
$ mkdir -p modules/h5 && cd modules/h5
$ mkdir controllers
$ touch Module.php
Copy after login

2.

Module. php Content example:

<?php
namespace frontend\modules\h5;

class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();

        $this->params[&#39;foo&#39;] = &#39;bar&#39;;
        // ...  其他初始化代码 ...
    }
}
Copy after login

3. Add module declaration in

frontend/config/main.php:

&#39;modules&#39; => [
    &#39;h5&#39; => [
        &#39;class&#39; => &#39;frontend\modules\h5\Module&#39;,
        // ... 模块其他配置 ...
    ],
],
Copy after login

4. In

modules/ h5/controllersNew controller class:

<?php
namespace frontend\modules\h5\controllers;

use Yii;
use common\models\LoginForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;

class SiteController extends Controller
{
    public function actionIndex()
    {
        return "hello h5 module";
        //return $this->render(&#39;index&#39;);
    }
}
Copy after login

Browser access:

http://localhost:8888/index.php?r=h5/site/index to access .

There is another way to implement an access form similar to this URL route, such as

r=test/site/index. Just create a new subdirectory in the frontend/controllers directory called test, put the controller in it, and then change the namespace to

namespace frontend\controllers\test;
Copy after login

. This can be used for API version control, for example:

r=v1/site/index
r=v2/site/index
Copy after login
Originally published in:

http://www.cnblogs.com/52fhy/...

Related Recommended:

Basic concepts of Yii2 configuration

Detailed explanation of Yii2.0 execution process

How to introduce the Yii framework



The above is the detailed content of Yii multi-application multi-module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template