【问题标题】:Where to put templates in my MVC structure? And where to put defines?在我的 MVC 结构中将模板放在哪里?以及在哪里放置定义?
【发布时间】:2017-02-15 15:34:53
【问题描述】:

我的 MVC 中有以下结构:

1) app
    1. controllers
        Home.class.php
    2. core
        App.class.php
        Controller.class.php
    3. models
        User.class.php
    4. views
        - home
            index.php
        .htaccess
        init.php
2) public
    1. css
    2. js
    .htaccess
    index.php

现在我也想实现模板。我之前在我的项目中使用过以下代码。

<?php
    class Template
    {
        private $assignedValues = array();
        private $tpl;

        public function __construct($_path = '')
        {
            if(!empty($_path)){
                if(file_exists($_path)){
                    $this->tpl = file_get_contents($_path);
                }
                else{
                    echo '<b>Template Error:</b> File Inclusion Error.';
                }
            }
        }

        public function assign($_searchString, $_replaceString)
        {
            if(!empty($_searchString)){
                $this->assignedValues[strtoupper($_searchString)] = $_replaceString;
            }
        }

        public function show()
        {
            if(count($this->assignedValues > 0)){
                foreach ($this->assignedValues as $key => $value) {
                    $this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
                }
            }

            echo $this->tpl;

        }

    }

那么我必须把这门课放在哪里?包含模板的文件夹在哪里?我还使用了带有定义的默认文件。放置我的定义文件的最佳位置是什么?

【问题讨论】:

    标签: php oop model-view-controller structure templating


    【解决方案1】:

    为什么不进去:

    app/views/layouts/your-template.php

    对我来说,将它与基于“视图”的结构的其余部分一起保留是很有意义的,因为它是一个模板。

    Laravel(一个 php 框架)也是这样做的。

    【讨论】:

    • 谢谢!处理模板的类? (在问题中给出)
    • 这需要更多的辩论。如果您将其扩展为大型框架,您可能会考虑在某处(例如 /app/lib 或 /app/src)启动 /lib 或 /src 文件夹。我认为现在 Template.class.php 可以进入 /app/core 文件夹。看起来这个类被设计成你的“核心”框架功能的一部分。
    猜你喜欢
    • 2015-07-25
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2015-10-23
    相关资源
    最近更新 更多