Yii 框架中控制器通过目录和命名空间实例化

发布于 2024-10-10 04:27:47 字数 459 浏览 0 评论 0原文

Yii 框架支持模块以及控制器目录中的子目录,因此某些特定操作的路径可以是

  • /index.php?r=module/controller/action
  • /index.php?r=subdirectoryInControllerDir /控制器/操作

我的目标是在控制器目录中有多个子目录。在这些文件夹中,我将使用命名空间创建与父级同名的控制器。

但是,如果我写

namespace mynamespace;
class MyController extends \MyController {
}

Yii 会加载 MyController 而不是 mynamespace\MyController;

这里有什么建议吗?

Yii framework supports modules and also subdirectories in controllers directory, so path to some specific action could be

  • /index.php?r=module/controller/action or
  • /index.php?r=subdirectoryInControllerDir/controller/action.

My goal here is to have multiple subdirectories in the controllers directory. Inside these folders I would create controllers with the same names as parent ones using namespaces.

However if I write

namespace mynamespace;
class MyController extends \MyController {
}

Yii would load MyController instead of mynamespace\MyController;

Any suggestions here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-10-17 04:27:47

Yii 对命名空间使用直观的命名约定,它从 \application 开始,并且必须遵循物理目录结构,就像内置的自动加载配置一样。
如果您的 MyController 基类位于 protected/controllers/ 中,那么它应该使用命名空间 application\controllers;

<?php
namespace application\controllers;
class MyController extends \CController
{
    // actions
}

和 protected/controllers/subdir/ 中的子 MyController

<?php
namespace application\controllers\subdir;
class MyController extends \application\controllers\MyController
{
    // actions
}

来发出类似“subdir/my”的请求,您需要在包含类文件后立即将以下代码添加到CWebApplication::createController()(或在子类中继承它):

    if(!class_exists($className,false))
        require($classFile);
+   if(!class_exists($className,false))
+       $className = '\\application\\controllers\\' . str_replace('/', '\\', $controllerID . $className);
    if(class_exists($className,false) && is_subclass_of($className,'CController'))
    {
        $id[0]=strtolower($id[0]);
        return array(
            new $className($controllerID.$id,$owner===$this?null:$owner),
            $this->parseActionParams($route),
        );
    }

如果您设置了controllerNameSpace对于 CWebApplication,您还可以使用该值而不是硬编码 \\application\\controllers\\

Yii uses an intuitive naming convention for namespaces, which starts from \application and must follow the physical directory structure, like the built-in autoload config.
If your base MyController class is in protected/controllers/, then it should use namespace application\controllers;

<?php
namespace application\controllers;
class MyController extends \CController
{
    // actions
}

and the child MyController in protected/controllers/subdir/

<?php
namespace application\controllers\subdir;
class MyController extends \application\controllers\MyController
{
    // actions
}

To make a request like "subdir/my" work, you need to add the following code to CWebApplication::createController() (or inherit it in a subclass) right after the class file is included:

    if(!class_exists($className,false))
        require($classFile);
+   if(!class_exists($className,false))
+       $className = '\\application\\controllers\\' . str_replace('/', '\\', $controllerID . $className);
    if(class_exists($className,false) && is_subclass_of($className,'CController'))
    {
        $id[0]=strtolower($id[0]);
        return array(
            new $className($controllerID.$id,$owner===$this?null:$owner),
            $this->parseActionParams($route),
        );
    }

If you have set controllerNameSpace of CWebApplication you can also use that value instead of hardcoding \\application\\controllers\\.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文