Rumah > rangka kerja php > YII > yii框架如何访问自定义模块下的controller

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

王林
Lepaskan: 2020-02-26 14:42:27
asal
2581 orang telah melayarinya

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

问题:

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

687f0727239ace4ea603e74cf2347c1.png

解决方法:

1、建立目录

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

<?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
    }
}
Salin selepas log masuk

(推荐教程:yii框架

2、web.php

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

<?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;
Salin selepas log masuk

3、api组件下的controllers

现在我们在Modules/api/controllers下新建一个SiteControllers.php,内容如下:

<?php

namespace app\modules\api\controllers;

use yii\web\Controller;


class SiteController extends Controller
{
    public function actionIndex()
    {
        echo "hello world";
    }
}
Salin selepas log masuk

4、浏览器访问

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

9a1164439726e272e764ca98b54c993.png

完成!

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

Atas ialah kandungan terperinci yii框架如何访问自定义模块下的controller. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan