zend multisites的目录结构

时间:2014-04-29 11:33:23

标签: php zend-framework zend-framework2

我需要使用将在子域上运行的zf2创建多站点。因此,任何人都可以建议需要创建的目录结构,并且不能为每个子域网站反复写入代码,以获得与多站点共享的相同功能。< / p>

请告知。

1 个答案:

答案 0 :(得分:0)

我在ZF1中编写了一个多站点应用程序,这是我使用的文件夹结构。

application
    configs
        sites
            domingod    <-- these
            artofwood   <-- are
            freedoms    <-- sites
        application.ini
    layouts
        scripts
            sites
                domingod
                artofwood
                freedoms
                    default.phtml    <-- Sites layout
        default.phtml    <-- default layout used if sites layout cannot be found
        admin.phtml      <-- admin layout
    modules
        default
            controllers
            forms
            models
            views
                helpers
                scripts
                    index
                        index.phtml    <-- Default layout file
                    sites
                        domingod
                        artofwood
                        freedoms
                            index
                                index.phtml    <-- Sites layout file, overrides default layout above
            Bootstrap.php
        ...

为了让这个工作,我需要使用几个插件。

首先我从数据库中获取该站点,我使用doctrine ORM,因此您可能需要将其更改为使用zend db。

class FreedomS_Zend_Controller_Plugin_GetSite extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $this->_httpHost    = $request->getHttpHost();
        $this->_scheme      = $request->getScheme();

        $httpHostArray = explode('.', $this->_httpHost);
        $this->_prefix = array_shift($httpHostArray);
        $this->_domainName = array_shift($httpHostArray);
        $this->_topLevelDomain = implode('.', $httpHostArray);


        $this->_fullDomainName = $this->_domainName . '.' . $this->_topLevelDomain;
        Zend_Registry::set('prefix',            $this->_prefix);
        Zend_Registry::set('domainName',        $this->_domainName);
        Zend_Registry::set('fullDomainName',    $this->_fullDomainName);
        Zend_Registry::set('topLevelDomain',    $this->_topLevelDomain);
        Zend_Registry::set('scheme',            $this->_scheme);
        Zend_Registry::set('baseUrl',           $this->_scheme . '://' . $this->_httpHost . '/');

        $site = Model_Doctrine_SitesTable::getInstance()->getOneByDomain($this->_fullDomainName); // Get site from database using doctrine

        if (is_array($site) && array_key_exists('siteId', $site)) {
            Zend_Registry::set('siteId', $site['siteId']);
            Zend_Registry::set('site', $site);
            $currency = new Zend_Currency($site['locale']); // get currency for site
            Zend_Registry::set('Zend_Currency', $currency);
        } else {
            Zend_Registry::set('siteId', null);
            Zend_Registry::set('site', array());
        }
    }
}

其次我需要ZF来读取网站的正确文件,请注意网站ID通过上面的插件存储在zend注册表中。我已经使用字符串作为站点id,例如自由和domingod,这使得目录结构中的内容更具可读性。

class FreedomS_Zend_Controller_Plugin_LayoutPicker extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();

          Zend_Layout::getMvcInstance()->getView()->headMeta()->appendHttpEquiv('Content-Type',  'text/html;charset=utf-8');

        switch ($module) {
            case 'author': // author and admin have access to the admin area and have a different layout to the sites
            case 'admin':
                Zend_Layout::getMvcInstance()->setLayout('admin');

                Zend_Layout::getMvcInstance()->getView()->headTitle('Direct Sites Admin');

                Zend_Layout::getMvcInstance()->getView()->doctype('XHTML1_STRICT');

                Zend_Layout::getMvcInstance()->getView()->headLink(Array('rel' => 'icon', 'href' => '/images/direct_links_icon.gif', 'type' => 'image/x-icon'));
                Zend_Layout::getMvcInstance()->getView()->headLink(Array('rel' => 'shortcut icon', 'href' => '/images/direct_links_icon.gif', 'type' => 'image/x-icon'));

                Zend_Layout::getMvcInstance()->getView()->headLink()->appendStylesheet('/css/admin.css');
                Zend_Layout::getMvcInstance()->getView()->headLink()->appendStylesheet('/css/sites/overlay.css');

                Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile('/js/jquery_1.7.2.js');
                Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile('/js/jquery.tools.min.js');
                Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile('/js/jquery-ui.min.js');
                Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile('/js/admin.js');

                $authStorage = new Zend_Session_Namespace('Zend_Auth');
                Zend_Layout::getMvcInstance()->getView()->username = $authStorage->storage['firstName'] . ' ' . $authStorage->storage['surname'];
                Zend_Layout::getMvcInstance()->getView()->date = date('l, j F Y');

                break;
            default:  // sites
                $layout = Model_Doctrine_LayoutsTable::getInstance()->getLayout(Zend_Registry::get('siteId'));  // get sites layout from database

                if (isset($layout['layout'])) {
                    $layoutFile = '/sites/' .  Zend_Registry::get('siteId') . '/' . $layout['layout'];

                    if (file_exists(APPLICATION_PATH . '/layouts/scripts' . $layoutFile . '.phtml')) {
                        Zend_Layout::getMvcInstance()->setLayout($layoutFile);
                    } else {
                        Zend_Layout::getMvcInstance()->setLayout('default');
                    }
                } else {
                    Zend_Layout::getMvcInstance()->setLayout('default');
                }

                if (isset($layout['docType'])) {
                    Zend_Layout::getMvcInstance()->getView()->doctype($layout['ZendDoctypes']['docType']);
                } else {
                    Zend_Layout::getMvcInstance()->getView()->doctype('XHTML1_STRICT');
                }

                if (isset($layout['title'])) {
                    Zend_Layout::getMvcInstance()->getView()->headTitle($layout['title']);
                }

                if (isset($layout['favIcon'])) {
                    Zend_Layout::getMvcInstance()->getView()->headLink(Array('rel' => 'icon', 'href' => $layout['favIcon'], 'type' => 'image/x-icon'));
                    Zend_Layout::getMvcInstance()->getView()->headLink(Array('rel' => 'shortcut icon', 'href' => $layout['favIcon'], 'type' => 'image/x-icon'));
                }

                if (isset($layout['Sites']['Metacontent']) && is_array($layout['Sites']['Metacontent']) && !empty($layout['Sites']['Metacontent'])) {
                    foreach ($layout['Sites']['Metacontent'] as $metatag) {
                        $methodName = $metatag['Metatags']['method'];
                        Zend_Layout::getMvcInstance()
                            ->getView()
                            ->headMeta()
                            ->$methodName($metatag['tagContent'], $metatag['content']);
                    }
                }

                if (isset($layout['Sites']['Scripts']) && is_array($layout['Sites']['Scripts']) && !empty($layout['Sites']['Scripts'])) {
                    foreach ($layout['Sites']['Scripts'] as $script) {
                        $scriptLink = '/js/' . $script['script'];
                        if (file_exists(APPLICATION_PATH . '/../public_html' . $scriptLink)) {
                            Zend_Layout::getMvcInstance()
                                ->getView()
                                ->headScript()
                                ->appendFile($scriptLink);
                        }
                    }
                }

                Zend_Layout::getMvcInstance()->getView()->headLink()->appendStylesheet('/css/sites/common.css');
                if (isset($layout['Sites']['Stylesheets']) && is_array($layout['Sites']['Stylesheets']) && !empty($layout['Sites']['Stylesheets'])) {
                    foreach ($layout['Sites']['Stylesheets'] as $stylesheet) {
                        $styleSheetLink =  '/css/sites/' . Zend_Registry::get('siteId') . '/' . $stylesheet['styleSheet'];
                        if (file_exists(APPLICATION_PATH . '/../public_html' . $styleSheetLink)) {
                            Zend_Layout::getMvcInstance()
                                ->getView()
                                ->headLink()
                                ->appendStylesheet($styleSheetLink);
                        } else {
                            $styleSheetLink =  '/css/sites/' . $stylesheet['styleSheet'];
                            if (file_exists(APPLICATION_PATH . '/../public_html' . $styleSheetLink)) {
                            Zend_Layout::getMvcInstance()
                                ->getView()
                                ->headLink()
                                ->appendStylesheet($styleSheetLink);
                            }
                        }
                    }
                }   
        }

    }
}

我的网站数据库表

siteId  varchar(32)
domain  varchar(255)
site    varchar(255)
emailAddress    varchar(255)
address     text 
telNo   varchar(32)    
welcomeText     varchar(50) 
vatNo   varchar(12) 
vatRate     tinyint(2)    
vatTitle    varchar(10)  
locale  varchar(5)

我的布局表

siteId  varchar(32)      
layout  varchar(32)
docTypeId   int(11)
title   varchar(100)
favIcon     varchar(255)

请记住这一切都在ZF1中,因此需要更换ZF2。

我希望这能指出你正确的方向。

亲切的问候

加里