beyerz/simple-hmvc-bundle

Symfony 扩展包,用于支持 HMVC 设计模式

安装次数: 3,846

依赖项: 0

建议者: 0

安全性: 0

星级: 1

关注者: 2

分支: 1

公开问题: 0

类型:symfony-bundle

v2.0.5 2019-04-14 16:15 UTC

This package is auto-updated.

Last update: 2024-09-15 04:36:41 UTC


README

Symfony2 的 Simple HMVC 扩展包

HMVC 设计模式是在尝试解决将小部件嵌入到页面中的问题时产生的。HMVC 允许我们清晰地区分控制器、模型和视图逻辑,从而使代码更容易工作,并更清晰地包含。

Symfony2 提供了工具,允许我们在控制器和视图中“加载”路由,但幕后过程比我喜欢的要复杂。

有关使用 symfony 提供的选项嵌入的信息,请参阅: https://symfony.com.cn/doc/2.8/book/templating.html#templating-embedding-controller

HMVC (层次化模型-视图-控制器) symfony 扩展包使您能够“小部件化”或在此扩展包中“元素化”您的控制器。在架构上,这允许您在多个页面中递归地包含相同元素一次或多次,而无需重建或重述模型逻辑。

安装

Composer(推荐)

composer require beyerz/simple-hmvc-bundle

应用程序内核

将 SimpleHMVC 添加到您应用程序内核的 registerBundles() 方法中

public function registerBundles()
{
    return array(
        new Beyerz\SimpleHMVCBundle\BeyerzSimpleHMVCBundle(),
    );
}

使用方法

页面和元素可以手动创建或使用提供的命令生成。如果您选择手动创建或需要手动更新,请考虑预期的文件结构。

创建页面

使用命令

php app/console hmvc:page

有关其他选项或帮助

php app/console hmvc:page --help

此命令将根据您的指定生成一个控制器和一个或多个动作。对于每个控制器动作,将创建以下类:{action}Model、{action}Context、{action}Input 和一个蛇形格式的视图。

创建页面将产生以下目录结构。此处使用的控制器名称为:BeyerzTestingBundle:GitExample

├── Context
│   └── Page
│       └── GitExample
│           ├── FirstExampleContext.php
│           └── SecondExampleContext.php
├── Controller
│   └── Page
│       └── GitExampleController.php
├── Input
│   └── Page
│       └── GitExample
│           ├── FirstExampleInput.php
│           └── SecondExampleInput.php
├── Model
│   └── Page
│       └── GitExample
│           ├── FirstExampleModel.php
│           └── SecondExampleModel.php
├── Resources
│   ├── config
│   │   ├── routing.yml
│   │   └── services.yml
│   └── views
│       └── Page
│           └── GitExample
│               ├── first_example.html.twig
│               └── second_example.html.twig
└── Tests
    └── Controller
        └── Page
            └── GitExampleControllerTest.php

您也可以在创建时指定路径来在不同目录下创建控制器。一个简单的子控制器将生成以下文件结构。此处使用的控制器名称为:BeyerzTestingBundle:GitExample/SubExample

├── Context
│   └── Page
│       └── GitExample
│           └── SubExample
│               ├── FirstSubExampleContext.php
│               └── SecondSubExampleContext.php
├── Controller
│   └── Page
│       └── GitExample
│           └── SubExampleController.php
├── Input
│   └── Page
│       └── GitExample
│           └── SubExample
│               ├── FirstSubExampleInput.php
│               └── SecondSubExampleInput.php
├── Model
│   └── Page
│       └── GitExample
│           └── SubExample
│               ├── FirstSubExampleModel.php
│               └── SecondSubExampleModel.php
├── Resources
│   ├── config
│   │   ├── routing.yml
│   │   └── services.yml
│   └── views
│       └── Page
│           └── GitExample
│               └── SubExample
│                   ├── first_sub_example.html.twig
│                   └── second_sub_example.html.twig
└── Tests
    └── Controller
        └── Page
            └── GitExample
                └── SubExampleControllerTest.php

创建元素

元素与页面非常相似,只是不会为元素生成控制器。

使用命令

php app/console hmvc:element

有关其他选项或帮助

php app/console hmvc:element --help

此命令将生成以下类:{element}Model、{element}Context、{element}Input 和一个蛇形格式的视图。

创建元素将产生以下目录结构。此处使用的元素名称为:BeyerzTestingBundle:GitElement

├── Context
│   └── Element
│       └── GitElementContext.php
├── Controller
├── DependencyInjection
│   ├── BeyerzTestingExtension.php
│   └── Configuration.php
├── Input
│   └── Element
│       └── GitElementInput.php
├── Model
│   └── Element
│       └── GitElementModel.php
├── Resources
│   ├── config
│   │   ├── routing.yml
│   │   └── services.yml
│   └── views
│       └── Element
│           └── git_element.html.twig
└── Tests

您也可以在创建时指定路径来在不同目录下创建元素。一个简单的子元素将生成以下文件结构。此处使用的元素名称为:BeyerzTestingBundle:GitExample/SubElement

├── Context
│   └── Element
│       └── GitExample
│           └── SubElementContext.php
├── Controller
├── DependencyInjection
│   ├── BeyerzTestingExtension.php
│   └── Configuration.php
├── Input
│   └── Element
│       └── GitExample
│           └── SubElementInput.php
├── Model
│   └── Element
│       └── GitExample
│           └── SubElementModel.php
├── Resources
│   ├── config
│   │   ├── routing.yml
│   │   └── services.yml
│   └── views
│       └── Element
│           └── GitExample
│               └── sub_element.html.twig
└── Tests

工作原理

一些有用的文章