zoukankan      html  css  js  c++  java
  • YII自定义控制器(基础版)

    Yii2访问自定义模块下的controller
    之前,由于所要访问的controller都是位于根目录下的controllers目录下,就像下面这样:

    此时,我们可以直接通过 localhost/basic/web/index.php?r=dao/index 来访问图中DaoController.php里的actionIndex。
    但是如果,我们将controller、view等有关联的独立出来作为一个模块Modules,那么又将怎样去处理呢。

    就像上面这样,这个Site控制器里面的action又将如何访问呢。
    1.建立目录
    首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:

    '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' => 'yiicachingFileCache', ], 'user' => [ 'identityClass' => 'appmodelsUser', 'enableAutoLogin' => true, ], 'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => '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' => true, ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yiilogFileTarget', 'levels' => ['error', 'warning'], ], ], ], 'db' => $db, /* 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ], */ ], 'modules' => [ 'api' => [ 'class' => 'appmodulesapiModule', ], ], 'params' => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yiidebugModule', // 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' => 'yiigiiModule', // uncomment the following to add your IP if you are not connecting from localhost. //'allowedIPs' => ['127.0.0.1', '::1'], ]; } return $config; 为了避免看的时候出错,上面红字的“键”是出于同一级的,红字加粗的是我们为modules添加的相关配置信息,相当于在应用中注册了刚才定义的组件(api)。 3.api组件下的controllers 现在我们在Modules/api/controllers下新建一个SiteControllers.php,内容如下:
  • 相关阅读:
    Java字符串跟ASCII码互转
    java 一款可以与ssm框架完美整合的web报表控件
    使用<c:set>标签配置项目路径
    Linux下部署tomcat及tomcat war包应用程序
    支付宝app支付服务端流程
    文本数据增量导入到mysql
    java 读取mysql中数据 并取出
    实现读取文本数据,在将数据导入mysql
    给一个整形数组,给出一个值,当这个值是数组某些数字的和,求出数组下标的值
    文本数据和mysql 里面的数据比较
  • 原文地址:https://www.cnblogs.com/AbbyXie/p/11526148.html
Copyright © 2011-2022 走看看