新闻资讯

新闻资讯 行业动态

框架yii如何访问自定义模块下的controller

编辑:011     时间:2020-02-26

问题:

Site控制器里面的action如何访问?如图:


解决方法:

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

 

namespaceapp\modules\api;

 

/**

 * api module definition class

 */

classModuleextends\yii\base\Module

{

    /**

     * @inheritdoc

     */

    public$controllerNamespace='app\modules\api\controllers';

 

    /**

     * @inheritdoc

     */

    publicfunctioninit()

    {

        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__ .'/params.php';

$db=require__DIR__ .'/db.php';

 

$config= [

    'id'=>'basic',

    'basePath'=> dirname(__DIR__),

    'bootstrap'=> ['log'],

    'aliases'=> [

        '@bower'=>'@vendor/bower-asset',

        '@npm'  =>'@vendor/npm-asset',

    ],

    'components'=> [

        'request'=> [

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

            'cookieValidationKey'=>'jjsYJ_ju0W8ifOv5mY3JBMI6DOppFlo6',

        ],

        'cache'=> [

            'class'=>'yii\caching\FileCache',

        ],

        'user'=> [

            'identityClass'=>'app\models\User',

            'enableAutoLogin'=> true,

        ],

        'errorHandler'=> [

            'errorAction'=>'site/error',

        ],

        'mailer'=> [

            'class'=>'yii\swiftmailer\Mailer',

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

            // 'useFileTransport' to false and configure a transport

            // for the mailer to send real emails.

            'useFileTransport'=> true,

        ],

        'log'=> [

            'traceLevel'=> YII_DEBUG ? 3 : 0,

            'targets'=> [

                [

                    'class'=>'yii\log\FileTarget',

                    'levels'=> ['error','warning'],

                ],

            ],

        ],

        'db'=>$db,

        /*

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

            ],

        ],

        */

    ],

    'modules'=> [

        'api'=> [

            'class'=>'app\modules\api\Module',

        ],

    ],

    'params'=>$params,

];

 

if(YII_ENV_DEV) {

    // configuration adjustments for 'dev' environment

    $config['bootstrap'][] ='debug';

    $config['modules']['debug'] = [

        'class'=>'yii\debug\Module',

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

        //'allowedIPs' => ['127.0.0.1', '::1'],

    ];

 

    $config['bootstrap'][] ='gii';

    $config['modules']['gii'] = [

        'class'=>'yii\gii\Module',

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

        //'allowedIPs' => ['127.0.0.1', '::1'],

    ];

}

 

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

 

namespaceapp\modules\api\controllers;

 

useyii\web\Controller;

 

 

classSiteControllerextendsController

{

    publicfunctionactionIndex()

    {

        echo"hello world";

    }

}

4、浏览器访问

最后就是浏览器访问这个actionIndex了,浏览器输入: http://localhost/basic/web/index.php?r=api/site/index

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

回复列表

相关推荐