首页 > php框架 > > 正文

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

王林
发布: 2020-02-26 14:42:27
原创
2398 人浏览过

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

问题:

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

687f0727239ace4ea603e74cf2347c1.png

解决方法:

1、建立目录

首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:

登录后复制

(推荐教程:yii框架

2、web.php

还记得项目根目录下的config文件夹下有个web.php文件么,添加如下字段:

 '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,内容如下:

登录后复制

4、浏览器访问

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

9a1164439726e272e764ca98b54c993.png

完成!

更多编程相关内容,请关注php中文网编程入门栏目!

以上是yii框架如何访问自定义模块下的controller的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!