我们什么时候使用Element?我们什么时候使用Helper?我们什么时候使用View Cells?在 CakePHP 3 中

Kim*_*cks 1 cakephp cakephp-3.0

我正在使用 CakePHP 3.x

我正在尝试将 themeforest 主题换成 CakePHP 插件。

中途,我正在决定是否将 portlet 换成助手、元素或视图单元。

portlet html 代码看起来像这样:

                <!-- BEGIN SAMPLE FORM PORTLET-->
                <div class="portlet box yellow">
                    <div class="portlet-title">
                        <div class="caption">
                            <i class="fa fa-gift"></i> More Form Samples
                        </div>
                        <div class="tools">
                            <a href="" class="collapse">
                            </a>
                            <a href="#portlet-config" data-toggle="modal" class="config">
                            </a>
                            <a href="" class="reload">
                            </a>
                            <a href="" class="remove">
                            </a>
                        </div>
                    </div>
                    <div class="portlet-body">
                        <h4>Inline Form</h4>
                        <form class="form-inline" role="form">
                            <div class="form-group">
                                <label class="sr-only" for="exampleInputEmail2">Email address</label>
                                <input type="email" class="form-control" id="exampleInputEmail2" placeholder="Enter email">
                            </div>
                            <div class="form-group">
                                <label class="sr-only" for="exampleInputPassword2">Password</label>
                                <input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password">
                            </div>
                            <div class="checkbox">
                                <label>
                                <input type="checkbox"> Remember me </label>
                            </div>
                            <button type="submit" class="btn btn-default">Sign in</button>
                        </form>
                        <hr>
                        <h4>Inline Form With Icons</h4>
                        <form class="form-inline" role="form">
                            <div class="form-group">
                                <label class="sr-only" for="exampleInputEmail22">Email address</label>
                                <div class="input-icon">
                                    <i class="fa fa-envelope"></i>
                                    <input type="email" class="form-control" id="exampleInputEmail22" placeholder="Enter email">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="sr-only" for="exampleInputPassword42">Password</label>
                                <div class="input-icon">
                                    <i class="fa fa-user"></i>
                                    <input type="password" class="form-control" id="exampleInputPassword42" placeholder="Password">
                                </div>
                            </div>
                            <div class="checkbox">
                                <label>
                                <input type="checkbox"> Remember me </label>
                            </div>
                            <button type="submit" class="btn btn-default">Sign in</button>
                        </form>
                        <hr>
                        <h4>Horizontal Form</h4>
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="inputEmail1" class="col-md-2 control-label">Email</label>
                                <div class="col-md-4">
                                    <input type="email" class="form-control" id="inputEmail1" placeholder="Email">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword12" class="col-md-2 control-label">Password</label>
                                <div class="col-md-4">
                                    <input type="password" class="form-control" id="inputPassword12" placeholder="Password">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-4">
                                    <div class="checkbox">
                                        <label>
                                        <input type="checkbox"> Remember me </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <button type="submit" class="btn blue">Sign in</button>
                                </div>
                            </div>
                        </form>
                        <hr>
                        <h4>Horizontal Form With Icons</h4>
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="inputEmail12" class="col-md-2 control-label">Email</label>
                                <div class="col-md-4">
                                    <div class="input-icon">
                                        <i class="fa fa-envelope"></i>
                                        <input type="email" class="form-control" id="inputEmail12" placeholder="Email">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword1" class="col-md-2 control-label">Password</label>
                                <div class="col-md-4">
                                    <div class="input-icon right">
                                        <i class="fa fa-user"></i>
                                        <input type="password" class="form-control" id="inputPassword1" placeholder="Password">
                                    </div>
                                    <div class="help-block">
                                         with right aligned icon
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-4">
                                    <div class="checkbox">
                                        <label>
                                        <input type="checkbox"> Remember me </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <button type="submit" class="btn green">Sign in</button>
                                </div>
                            </div>
                        </form>
                        <hr>
                        <h4>Column Sizing</h4>
                        <form role="form">
                            <div class="row">
                                <div class="col-md-2">
                                    <input type="text" class="form-control" placeholder=".col-md-2">
                                </div>
                                <div class="col-md-3">
                                    <input type="text" class="form-control" placeholder=".col-md-3">
                                </div>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" placeholder=".col-md-4">
                                </div>
                                <div class="col-md-3">
                                    <input type="text" class="form-control" placeholder=".col-md-2">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <!-- END SAMPLE FORM PORTLET-->
Run Code Online (Sandbox Code Playgroud)

外观是这样的: 在此输入图像描述

我的问题是我们如何知道何时应该使用 Element?什么时候我们应该使用Helper?以及我们什么时候应该使用视图单元格?

对于上述情况我应该使用哪种情况?我倾向于Helper。

bur*_*zum 5

元素

当您需要大量重复与演示文稿相关的内容(通常是 HTML)时,请使用它。例如,我有一个项目,其中三个表使用地址表的记录。包含地址数据的所有这三个的形式部分是一个元素。

帮手

使用它来封装视图逻辑,如果可能的话不要将 HTML 或其他与表示相关的东西放入其中。例如,让它执行某些操作,然后根据结果,您可以使用该结果类型的元素来呈现数据:return $this->_view->render('items/' . $type . '_item');

如果您查看核心帮助程序(例如 HtmlHelper),您将看到一个属性$_defaultConfig

protected $_defaultConfig = [
    'templates' => [
    'meta' => '<meta{{attrs}}/>',
    'metalink' => '<link href="{{url}}"{{attrs}}/>',
    /*...*/
Run Code Online (Sandbox Code Playgroud)

这些是用于生成 HTML 输出的模板字符串。这将标记与生成它的实际代码很好地分开。也看看 FormHelper,它使用小部件来呈现更复杂的输出。

所以这对于像标记这样的元素来说效果很好。根据经验,我会说,如果您的标记比您在那里看到的更长,则将其作为一个元素,并从助手中调用它或将其作为一个小部件。

查看单元格

将视图单元视为具有视图并可以加载多个模型的“迷你 MVC”堆栈。恕我直言,如果您熟悉它们,它们与 AngularJS 指令类似。请参阅这篇文章的示例。我真的建议您阅读它,它详细解释了它们及其用例。

我还没有对它们做太多事情,但它们可以用来替换例如 requestAction() 调用。您不会使用不打算由请求访问的方法来“污染”您的控制器。

摘自上面链接的文章:

CakePHP 最常用的功能之一是 View::requestAction()。开发人员经常在他们的应用程序中使用这一点,从而导致复杂的情况,您需要弄清楚您是在 Web 请求还是内部操作请求中,从而使控制器变得混乱。您还需要调用新的 CakePHP 请求,这可能会增加一些不必要的开销。

免责声明

以上只是我个人对这些东西的看法,没有最终的规则你必须如何使用这三个东西。目标始终是干净且可重用的代码以及适当的关注点分离。如何归档取决于您,您已经拥有了工具。:)