最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

yii框架如何访问自定义模块下的controller

来源:动视网 责编:小采 时间:2020-11-03 18:17:47
文档

yii框架如何访问自定义模块下的controller

yii框架如何访问自定义模块下的controller:问题:Site控制器里面的action如何访问?如图:解决方法:1、建立目录首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:<php namespace app\modules\api; /** * api module
推荐度:
导读yii框架如何访问自定义模块下的controller:问题:Site控制器里面的action如何访问?如图:解决方法:1、建立目录首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:<php namespace app\modules\api; /** * api module


问题:

Site控制器里面的action如何访问?如图:

解决方法:

1、建立目录

首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:

<?php

namespace appmodulesapi;

/**
 * api module definition class
 */
class Module extends yiiaseModule
{
 /**
 * @inheritdoc
 */
 public $controllerNamespace = 'appmodulesapicontrollers';

 /**
 * @inheritdoc
 */
 public function init()
 {
 parent::init();

 // custom initialization code goes here
 }
}

(推荐教程:yii框架)

2、web.php

还记得项目根目录下的config文件夹下有个web.php文件么,添加如下字段:

<?php

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
 'id' => '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;

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

完成!

更多编程相关内容,请关注Gxlcms编程入门栏目!

文档

yii框架如何访问自定义模块下的controller

yii框架如何访问自定义模块下的controller:问题:Site控制器里面的action如何访问?如图:解决方法:1、建立目录首先建立如上的目录结构,在api下的以及目录有三个文件夹和一个文件Module.php,这个php文件内容如下:<php namespace app\modules\api; /** * api module
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top