>  기사  >  PHP 프레임워크  >  Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?

Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?

王林
王林원래의
2020-02-26 14:42:272472검색

Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?

질문:

사이트 컨트롤러의 작업에 어떻게 액세스하나요? 그림과 같이:

Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?

해결책:

1. 디렉터리를 만듭니다.

먼저 위와 같이 디렉터리 구조를 만듭니다. api와 디렉터리에는 Module.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
    }
}

(추천 튜토리얼: yii Framework)

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;

3. api 컴포넌트 컨트롤러

이제 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";
    }
}

4. 마지막으로 브라우저는 이 actionIndex에 액세스합니다. , 브라우저는 http://localhost/basic/web/index.php?r=api/site/index

Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?완료!

프로그래밍과 관련된 더 많은 내용은 PHP 중국어 홈페이지

프로그래밍 입문

칼럼을 주목해주세요!

위 내용은 Yii 프레임워크는 사용자 정의 모듈 아래의 컨트롤러에 어떻게 액세스합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.