Home > PHP Framework > YII > body text

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

王林
Release: 2020-02-26 14:42:27
Original
2397 people have browsed it

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:

Copy after login

(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:

 '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;
Copy after login

3. Controllers under the api component

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

Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!