首頁 > php框架 > YII > yii框架如何存取自訂模組下的controller

yii框架如何存取自訂模組下的controller

王林
發布: 2020-02-26 14:42:27
原創
2653 人瀏覽過

yii框架如何存取自訂模組下的controller

問題:

Site控制器裡面的action如何存取?如圖:

yii框架如何存取自訂模組下的controller

解決方法:

1、建立目錄

首先建立如上的目錄結構,在api下的以及目錄有三個資料夾和一個檔案Module.php,這個php檔案內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?php

 

namespace app\modules\api;

 

/**

 * api module definition class

 */

class Module extends \yii\base\Module

{

    /**

     * @inheritdoc

     */

    public $controllerNamespace = &#39;app\modules\api\controllers&#39;;

 

    /**

     * @inheritdoc

     */

    public function init()

    {

        parent::init();

 

        // custom initialization code goes here

    }

}

登入後複製

(推薦教學:yii框架

#2、web.php

還記得專案根目錄下的config資料夾下有個web.php文件麼,加入以下欄位:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

<?php

 

$params = require __DIR__ . &#39;/params.php&#39;;

$db = require __DIR__ . &#39;/db.php&#39;;

 

$config = [

    &#39;id&#39; => &#39;basic&#39;,

    &#39;basePath&#39; => dirname(__DIR__),

    &#39;bootstrap&#39; => [&#39;log&#39;],

    &#39;aliases&#39; => [

        &#39;@bower&#39; => &#39;@vendor/bower-asset&#39;,

        &#39;@npm&#39;   => &#39;@vendor/npm-asset&#39;,

    ],

    &#39;components&#39; => [

        &#39;request&#39; => [

            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation

            &#39;cookieValidationKey&#39; => &#39;jjsYJ_ju0W8ifOv5mY3JBMI6DOppFlo6&#39;,

        ],

        &#39;cache&#39; => [

            &#39;class&#39; => &#39;yii\caching\FileCache&#39;,

        ],

        &#39;user&#39; => [

            &#39;identityClass&#39; => &#39;app\models\User&#39;,

            &#39;enableAutoLogin&#39; => true,

        ],

        &#39;errorHandler&#39; => [

            &#39;errorAction&#39; => &#39;site/error&#39;,

        ],

        &#39;mailer&#39; => [

            &#39;class&#39; => &#39;yii\swiftmailer\Mailer&#39;,

            // send all mails to a file by default. You have to set

            // &#39;useFileTransport&#39; to false and configure a transport

            // for the mailer to send real emails.

            &#39;useFileTransport&#39; => true,

        ],

        &#39;log&#39; => [

            &#39;traceLevel&#39; => YII_DEBUG ? 3 : 0,

            &#39;targets&#39; => [

                [

                    &#39;class&#39; => &#39;yii\log\FileTarget&#39;,

                    &#39;levels&#39; => [&#39;error&#39;, &#39;warning&#39;],

                ],

            ],

        ],

        &#39;db&#39; => $db,

        /*

        &#39;urlManager&#39; => [

            &#39;enablePrettyUrl&#39; => true,

            &#39;showScriptName&#39; => false,

            &#39;rules&#39; => [

            ],

        ],

        */

    ],

    &#39;modules&#39; => [

        &#39;api&#39; => [

            &#39;class&#39; => &#39;app\modules\api\Module&#39;,

        ],

    ],

    &#39;params&#39; => $params,

];

 

if (YII_ENV_DEV) {

    // configuration adjustments for &#39;dev&#39; environment

    $config[&#39;bootstrap&#39;][] = &#39;debug&#39;;

    $config[&#39;modules&#39;][&#39;debug&#39;] = [

        &#39;class&#39; => &#39;yii\debug\Module&#39;,

        // uncomment the following to add your IP if you are not connecting from localhost.

        //&#39;allowedIPs&#39; => [&#39;127.0.0.1&#39;, &#39;::1&#39;],

    ];

 

    $config[&#39;bootstrap&#39;][] = &#39;gii&#39;;

    $config[&#39;modules&#39;][&#39;gii&#39;] = [

        &#39;class&#39; => &#39;yii\gii\Module&#39;,

        // uncomment the following to add your IP if you are not connecting from localhost.

        //&#39;allowedIPs&#39; => [&#39;127.0.0.1&#39;, &#39;::1&#39;],

    ];

}

 

return $config;

登入後複製

3、api元件下的controllers

現在我們在Modules/api/controllers下新建一個SiteControllers.php,內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?php

 

namespace app\modules\api\controllers;

 

use yii\web\Controller;

 

 

class SiteController extends Controller

{

    public function actionIndex()

    {

        echo "hello world";

    }

}

登入後複製

4、瀏覽器訪問

最後就是瀏覽器訪問這個actionIndex了,瀏覽器輸入: http:// localhost/basic/web/index.php?r=api/site/index

yii框架如何存取自訂模組下的controller

完成!

更多程式相關內容,請關注php中文網程式入門欄位!

以上是yii框架如何存取自訂模組下的controller的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板