Yii 框架學習 03 多應用多模組

2022-02-01 01:27:36 字數 2144 閱讀 6970

本文以yii 2.0.7為例。

首先看看多應用和多模組的特點:

多應用的特點:

多模組的特點:

那麼,實際該怎麼決定使用多應用還是多模組呢?

# windows

init.bat

# linux

init

會在frontendbackend兩個應用的web目錄生成入口檔案index.phpfrontendbackend分別表示前台和後台應用,裡面的目錄結構是一樣的:

assets/  

config/

controllers/

models/

runtime/

views/

web/

執行:

$ 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

開啟瀏覽器輸入就可以訪問預設的首頁了。

建議model還是放在根目錄的common/models裡。

多模組可以參照配置。示例:在frontend裡新建乙個h5應用:

1、建立相關目錄

$ cd frontend

$ mkdir -p modules/h5 && cd modules/h5

$ mkdir controllers

$ touch module.php

2、module.php內容示例:

<?php

namespace frontend\modules\h5;

class module extends \yii\base\module

}

3、在frontend/config/main.php增加模組的申明:

'modules' => [

'h5' => [

'class' => 'frontend\modules\h5\module',

// ... 模組其他配置 ...

],],

4、在modules/h5/controllers新建控制器類:

}瀏覽器訪問:http://localhost:8888/index.php?r=h5/site/index即可訪問。

還有一種方法也可以實現類似該url路由的訪問形式,例如r=test/site/index。只需要在frontend/controllers目錄新建個子目錄叫test,把控制器放在裡面,然後改下命名空間為

namespace frontend\controllers\test;
就可以了。這種可以用於api版本控制,例如:

r=v1/site/index

r=v2/site/index

Yii2 建立多應用

yii2.0的高階應用模板包括三個預設的應用模板 在frontend同目錄新建乙個名為weixin資料夾.把frontend目錄下面的東西全拷貝到weixin目錄裡面 修改web目錄index.php的包含路徑,還有config main.php的包含路徑 修改config main.php配置項c...

Struts 之旅 配置多應用模組

配置兩個或以上的struts config.xml 一下內容只是按照我自己的理解去做的乙個小專案 建立兩個 struts config.xml 和 struts configb.xml 只是為了測試 我就寫了乙個action and form 在web.xml中把第二個struts configb....

聊聊 php yaf框架擴充套件實踐二 多模組

雖然多模組的配置可能會有一些爭議,但是在專案前期能加快開發效率和部署效率,若專案發展起來有了更多的資源後可以考慮分拆模組。yaf也支援多模組的配置,先看下yaf如何配置多模組。假如我們要新增加乙個api模組,用來提供給app調取使用 新增乙個admin模組用來做後台管理。配置檔案中增加 applic...