Php Yii2:模块中的独立操作
我想创建一个目录树,如下所示:Php Yii2:模块中的独立操作,php,yii,namespaces,action,yii2,Php,Yii,Namespaces,Action,Yii2,我想创建一个目录树,如下所示: 模型 模块 帮助 控制器 违约 index.php DefaultController.php 观点 违约 index.php help.php 因此,如果我调用localhost/mysite/web/index.php?r=help/default/index,我应该会得到索引视图 所以我用gii创建了一个模块。我的模块类是“app\modules\help\help”。我必须使用两次帮助,以便在子目录中获取模块。(这是我不明白的。
- 模型
- 模块
- 帮助
- 控制器
- 违约
- index.php
- DefaultController.php
- 违约
- 观点
- 违约
- index.php
- 违约
- help.php
- 控制器
- 帮助
<?php
namespace app\modules\help\controllers;
use yii\base\Action;
class IndexAction extends Action
{
public function run()
{
$this->controller->render('index');
}
}
但是如果我在我的电脑上走到这个路径E:\wamp\www\my_website/modules/help/controllers/default/,我会看到IndexAction.php
有人能帮我吗
独立操作位于控制器的子目录中。所以名称空间应该是
namespace app\modules\help\controllers\default;
但这导致了这个错误:
PHP Parse Error – yii\base\ErrorException
syntax error, unexpected 'default' (T_DEFAULT), expecting identifier (T_STRING)
编辑
如果在IndexAction.php中使用此名称空间app\modules\help\controllers\IndexAction,则会出现以下错误。即使我在控制器中更改了路由,我也会得到未知的异常:
Unknown Class – yii\base\UnknownClassException
Unable to find 'app\modules\help\controllers\default\IndexAction' in file: E:\wamp\www\my_website/modules/help/controllers/default/IndexAction.php. Namespace missing?
如果我在IndexAction.php中使用这个名称空间app\modules\help\controllers\default,我会得到:
PHP Parse Error – yii\base\ErrorException
syntax error, unexpected 'default' (T_DEFAULT), expecting identifier (T_STRING)
您使用了错误的名称空间 将代码替换为以下内容(注意反斜杠)
正如D.米尔在评论中所说的那样默认值是保留关键字。我不得不重新命名这个目录,现在它运行得很好 即使我使用前导反斜杠,它也不起作用。我认为错误消息中的斜杠是正面斜杠,而反斜杠是由WAMP引起的。它不起作用意味着,我得到了一个未知的异常,就像我的问题中显示的那样。您是否尝试过
app\modules\help\controllers\IndexAction
?或者将操作的名称空间更改为app\modules\help\controllers\default
答案显示在上面的“编辑”语句中的勾号中编辑的第二部分是正确的方式。错误源于default
是保留关键字(属于开关
语句的一部分)。如果您将它(和文件夹名称)更改为其他内容,它应该可以工作。您是对的。如果我将default重命名为index,它就会工作。
Unknown Class – yii\base\UnknownClassException
Unable to find 'app\modules\help\controllers\default\IndexAction' in file: E:\wamp\www\my_website/modules/help/controllers/default/IndexAction.php. Namespace missing?
PHP Parse Error – yii\base\ErrorException
syntax error, unexpected 'default' (T_DEFAULT), expecting identifier (T_STRING)
public function actions()
{
return [
'index' => '\app\modules\help\controllers\default\IndexAction',
];
}