如何在phalcon中创建多站点设置与一些共享控制器,模型,视图与重复视图文件夹?

时间:2016-08-11 10:16:55

标签: phalcon multisite

我需要在phalcon中设置多站点,我需要在所有站点之间完成一些常用功能,并且还要具有站点特定功能。假设它有一些常见的控制器模态和视图,如果我需要在一个特定的站点中更改任何内容,我应该能够在该特定站点中进行更改而不影响其他站点。只需创建单个视图模板并扩展控制器和模态。如果我需要更改所有网站中的任何内容,那么我可以在一个地方更改它。

1 个答案:

答案 0 :(得分:1)

    multisite/shared
    ├── apps
    │   ├── common
    │   │   ├── controllers        (Register namespace Common/Controller)
    │   │   │   ├── IndexController.php   
    │   │   │   ├── LoginController.php
    │   │   │   └── ProductsController.php
    │   │   ├── models             (Register namespace Common/Model)
    │   │   │   └── Products.php  
    │   │   └── views
    │   │       ├── login
    │   │       │   └── index.volt
    │   │       └── products
    │   │       |    └── index.volt
    |   |       └──index.volt   
    │   ├── example.com
    │   │   ├── controllers
    │   │   │   ├── IndexController.php (extend Common/Controller)
    │   │   │   ├── LoginController.php  (extend Common/Controller)
    │   │   │   ├── ProductsController.php (extend Common/Controller)
    │   │   │   └── UsersController.php   Site Specific Controller
    │   │   ├── models
    │   │   │   └── Products.php (extend Common/Model)
    |   |   |   └── Users.php (Site Specific Model)
    │   │   └── views
    │   │       └── products             (Other view templates will refer to Common view folder)
    │   │           └── index.volt
    │   ├── example2.com
    │   │   ├── controllers
    │   │   │   ├── IndexController.php (extend Common/Controller)
    │   │   │   ├── ProductsController.php (extend Common/Controller)
    │   │   │   └── SitespecificController.php   Site Specific Controller
    │   │   ├── models
    │   │   │   └── Products.php (extend Common/Model)
    |   |   |   └── SiteSpecific.php (Site Specific Model)
    │   │   └── views
    │   │       └── sitespecific        (Other view templates will refer to Common view folder)
    │   │           └── index.volt
    └── public
        └── example.com   (Will contain Js CS Images to support site specific theme)
        └── example2.com  (Will contain Js CS Images to support site specific theme)
        └── index.php

参考:http://monkpal.com/Multisite-Set-up-with-shared-views-controllers-and-modals-Phalcon

设置具有不同域名的多个网站的步骤

实现目标的步骤

步骤1:注册公共控制器和模型的名称空间

步骤2:扩展phalcon视图引擎以级联视图(例如,View引擎将查找特定于站点的视图文件夹中的特定模板文件,如果它不存在,它将在常见的视图文件夹中查找,不需要复制所有站点视图目录中的所有模板文件,都可以单独覆盖单个模板文件)。

步骤3:扩展Phalcon伏特以提供模板的皮肤路径步骤4:创建特定于站点的Volt缓存文件夹

步骤5:在公共文件夹中为js / css / images创建带有网站名称的单独文件夹步骤6:创建公共控制器,视图,模态

步骤7:扩展常用控制器,站点特定文件夹中的模态,视图将从公共文件夹中获取。如果你想覆盖任何模板,你可以覆盖它,不需要所有的视图文件夹。第8步:按当前域名设置网站名称。这个网站名称将用于注册控制器模型指令

步骤9:设置两个视图目录,一个是常见的,另一个是sitename(只有在你扩展了phalcon视图以添加两个目录才能完成这个步骤参考步骤2)扩展的文件在这里应该在你的公共目录中。

扩展的文件在这里,应该在您的根目录中。

定制/ CustomVolt.php

  <?php

 namespace Custom;

 use Phalcon\Mvc\View\Engine\Volt;
 use Phalcon\Mvc\View\Engine\Volt\Compiler;

 class CustomVolt extends Volt
 { 
      public function getCompiler()
     {
        if (!$this->_compiler) {
           $this->_compiler = new VoltCompilerExtension($this->getView());
           $this->_compiler->setOptions($this->getOptions());
           $this->_compiler->setDI($this->getDI());
       }
     return $this->_compiler;
    }
  }


 class VoltCompilerExtension extends Volt\Compiler
 {
     public function compileFile($path, $compiledPath, $extendsMode = null)
     {
         $skinPath = $this->getOption('skinPath');
          if ($skinPath) {
             $skinTemplate = str_replace(
             $this->getDI()->getView()->getViewsDir(),
             $skinPath,
             $path);

            if (is_readable($skinTemplate)) {
              $path = $skinTemplate;
            }
          }
         return parent::compileFile($path, $compiledPath, $extendsMode);
      }

 }

定制/ CustomView.php

 use Phalcon\Mvc\View\Exception;
 use Phalcon\Mvc\View;
 use Phalcon\Cache\BackendInterface;

 class CustomView extends View
 {
     protected $_viewsDirs;
     /**
     * @var
     */
     protected $_eventsManager;
    /**
     * @param $path
      *
     * @return $this
     */
      public function addViewsDir($path)
     {
       $this->_viewsDirs = $path;
       $this->setViewsDir($path);
       return $this;
     }
     /**
     * @param $view
     * @param array $vars
     *
     * @return string
     */
     public function getPartial($view, $vars = [])
     {
         ob_start();
         $this->partial($view, $vars);
      $content = ob_get_contents();
      ob_end_clean();
         return $content;
      }

      protected function _engineRender($engines, $viewPath, $silence, $mustClean, BackendInterface $cache = NULL)
      {
        if (is_object($cache)) {
            throw new Exception('Cache view not supported...');
            return;
        }
        $viewsDirs = is_array($this->_viewsDirs) ? array_reverse($this->_viewsDirs) : [$this->_viewsDir];
        $notExists = true;
        $viewEnginePath = null;
        foreach ($engines as $extension => $engine) {
            foreach ($viewsDirs as $viewsDir) {
                $viewsDirPath   = $this->_basePath . $viewsDir . $viewPath;
                $viewEnginePath = $viewsDirPath . $extension;
                if (is_file($viewEnginePath)) {
                    if (is_object($this->_eventsManager)) {
                        $this->_activeRenderPath = $viewEnginePath;
                        if($this->_eventsManager->fire('view:beforeRenderView', $this, $viewEnginePath) === false) {
                            break;
                        }
                    }
                    $engine->render($viewEnginePath, $this->_viewParams, $mustClean);
                    if (is_object($this->_eventsManager)) {
                        $this->_eventsManager->fire('view:afterRenderView', $this);
                    }
                    $notExists = false;
                    break 2;
                }
            }
        }
        if ($notExists) {
            if (is_object($this->_eventsManager)) {
                $this->_activeRenderPath = $viewEnginePath;
                $this->_eventsManager->fire('view:notFoundView', $this);
            }
            if (!$silence) {
                $exceptionMessage = 'View "'.($viewPath).'" was not found in the views directories';
                throw new Exception($exceptionMessage);
                return;
               }
            }
        }
}

公共/ index.php的

<?php
use Phalcon\Loader;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Custom\CustomVolt;
use Custom\CustomView;


if($_SERVER['HTTP_HOST'] == "example.com") {
  define('SITENAME',"example.com" );
}

if($_SERVER['HTTP_HOST'] == "example2.com") {
  define('SITENAME',"example2.com" );
}  

define('APP_PATH', realpath('..') . '/');
try {

  $loader = new Loader();
  $loader->registerNamespaces(array(
  'Common\Controller' => '../app/common/controllers',
  'Common\Model' => '../app/common/models',
  'Custom'  => 'custom'
  ))->register();

$loader->registerDirs(array(
'../app/'.SITENAME.'/controllers/',

'../app/'.SITENAME.'/models/'
))->register();

$di = new FactoryDefault();

$di->set(
  'voltService',
    function ($view, $di) {
      $volt = new CustomVolt($view, $di);
      $volt->setOptions(
        array(
        "compiledPath"      => "../cache/volt/".SITENAME."/",
        "compiledExtension" => ".compiled",
        'compileAlways' => true,
        'skinPath' => '../app/'.SITENAME.'/views/'
        )
      );
  return $volt;
  }
);

$di->set(
'view',
  function () {
    $view = new CustomView();
    $view->addViewsDir(array('../app/common/views/','../app/'.SITENAME.'/views/'));
    $view->registerEngines(
      array(
      ".volt" => 'voltService'
      )
    );
    return $view;
  }
);

$application = new Application($di);
$response = $application->handle();
$response->send();

}
catch (\Exception $e) {
  echo "Exception: ", $e->getMessage();
}  

以伏特tempaltes呈现特定的Js和css站点

你可以这样使用

{{ stylesheet_link(constant('SITENAME') ~'/css/main.css') }}    
{{ javascript_include(constant('SITENAME') ~'/js/main.js') }}