Home  >  Article  >  PHP Framework  >  How does the Yii framework access the controller under the custom module?

How does the Yii framework access the controller under the custom module?

王林
王林Original
2020-02-26 14:42:272518browse

How does the Yii framework access the controller under the custom module?

Question:

How to access the actions in the Site controller? As shown in the picture:

How does the Yii framework access the controller under the custom module?

Solution:

1. Create the directory

First create the directory structure as above, and the directory under the api There are three folders and one file Module.php. The content of this php file is as follows:

<?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
    }
}

(Recommended tutorial: yii framework)

2. web.php

Remember that there is a web.php file in the config folder in the root directory of the project? Add the following fields:

<?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. Controllers under the api component

Now we are Create a new SiteControllers.php under Modules/api/controllers with the following content:

<?php

namespace app\modules\api\controllers;

use yii\web\Controller;


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

4. Browser access

Finally, the browser accesses this actionIndex. The browser enters: http:// localhost/basic/web/index.php?r=api/site/index

How does the Yii framework access the controller under the custom module?

Done!

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of How does the Yii framework access the controller under the custom module?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn