yii框架如何訪問自定義模塊下的controller

yii框架如何訪問自定義模塊下的controller

問題:

Site控制器里面的action如何訪問?如圖:

yii框架如何訪問自定義模塊下的controller

解決方法:

1、建立目錄

首先建立如上的目錄結構,在api下的以及目錄有三個文件夾和一個文件Module.php,這個php文件內容如下:

<?php namespace appmodulesapi;  /**  * api module definition class  */ class Module extends YIIbaseModule {     /**      * @inheritdoc      */     public $controllerNamespace = &#39;appmodulesapicontrollers&#39;;      /**      * @inheritdoc      */     public function init()     {         parent::init();          // custom initialization code goes here     } }

(推薦教程: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; =>?'basic', ????'basePath'?=&gt;?dirname(__DIR__), ????'bootstrap'?=&gt;?['log'], ????'aliases'?=&gt;?[ ????????'@bower'?=&gt;?'@vendor/bower-asset', ????????'@npm'???=&gt;?'@vendor/npm-asset', ????], ????'components'?=&gt;?[ ????????'request'?=&gt;?[ ????????????//?!!!?insert?a?secret?key?in?the?following?(if?it?is?empty)?-?this?is?required?by?cookie?validation ????????????'cookieValidationKey'?=&gt;?'jjsYJ_ju0W8ifOv5mY3JBMI6DOppFlo6', ????????], ????????'cache'?=&gt;?[ ????????????'class'?=&gt;?'yiicachingFileCache', ????????], ????????'user'?=&gt;?[ ????????????'identityClass'?=&gt;?'appmodelsUser', ????????????'enableAutoLogin'?=&gt;?true, ????????], ????????'errorHandler'?=&gt;?[ ????????????'errorAction'?=&gt;?'site/error', ????????], ????????'mailer'?=&gt;?[ ????????????'class'?=&gt;?'yiiswiftmailerMailer', ????????????//?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'?=&gt;?true, ????????], ????????'log'?=&gt;?[ ????????????'traceLevel'?=&gt;?YII_DEBUG???3?:?0, ????????????'targets'?=&gt;?[ ????????????????[ ????????????????????'class'?=&gt;?'yiilogFileTarget', ????????????????????'levels'?=&gt;?['error',?'warning'], ????????????????], ????????????], ????????], ????????'db'?=&gt;?$db, ????????/* ????????'urlManager'?=&gt;?[ ????????????'enablePrettyUrl'?=&gt;?true, ????????????'showScriptName'?=&gt;?false, ????????????'rules'?=&gt;?[ ????????????], ????????], ????????*/ ????], ????'modules'?=&gt;?[ ????????'api'?=&gt;?[ ????????????'class'?=&gt;?'appmodulesapiModule', ????????], ????], ????'params'?=&gt;?$params, ];  if?(YII_ENV_DEV)?{ ????//?configuration?adjustments?for?'dev'?environment ????$config['bootstrap'][]?=?'debug'; ????$config['modules']['debug']?=?[ ????????'class'?=&gt;?'yiidebugModule', ????????//?uncomment?the?following?to?add?your?IP?if?you?are?not?connecting?from?localhost. ????????//'allowedIPs'?=&gt;?['127.0.0.1',?'::1'], ????];  ????$config['bootstrap'][]?=?'gii'; ????$config['modules']['gii']?=?[ ????????'class'?=&gt;?'yiigiiModule', ????????//?uncomment?the?following?to?add?your?IP?if?you?are?not?connecting?from?localhost. ????????//'allowedIPs'?=&gt;?['127.0.0.1',?'::1'], ????]; }  return?$config;

3、api組件下的controllers

現在我們在Modules/api/controllers下新建一個SiteControllers.php,內容如下:

<?php namespace appmodulesapicontrollers;  use yiiwebController;   class SiteController extends Controller {     public function actionIndex()     {         echo "hello world";     } }

4、瀏覽器訪問

最后就是瀏覽器訪問這個actionIndex了,瀏覽器輸入:?http://localhost/basic/web/index.php?r=api/site/index

yii框架如何訪問自定義模塊下的controller

完成!

更多編程相關內容,請關注php中文網yii框架欄目!

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享