1

我使用 ZEND 框架版本 1.12.0 构建了我的网站。我的站点中还有其他三个模块(管理员、博客、图像),我想以http://admin.mysitename.comhttp://blog.mysitename.comhttp://images.mysitename.com的形式访问分别。但是,我无法访问子域。请注意:模块具有不同的操作以及更新、添加、存档等。

.ht 访问代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>

引导程序.php:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
    /**
     * Initialize the application autoload
     *
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAppAutoload(){
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'App',
            'basePath'  => dirname(__FILE__),
        ));
        return $autoloader;
    }

    /**
     * Initialize the layout loader
     */
    protected function _initLayoutHelper(){
        $this->bootstrap('frontController');
        $layout = Zend_Controller_Action_HelperBroker::addHelper(new Amz_Controller_Action_Helper_LayoutLoader());
    }   

    public function _initRoutes(){
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

        $route = new Zend_Controller_Router_Route ('cms/:page_url/',array('controller' => 'Cms','action'=> 'index'));
        $router->addRoute('cms-page', $route);


        $router->addRoute('user', new Application_Route_User(
            'user',
            array(
                'module' => 'default',
                'controller' => 'Userdetail',
                'action' => 'index'
            )
        ));


        $routeJoin = new Zend_Controller_Router_Route ('join/:id/',array('controller' => 'Index','action'=> 'index'));
        $router->addRoute('join', $routeJoin); 


        $registration = new Zend_Controller_Router_Route ('registration/invitation/:id',array('controller' => 'Registration','action'=> 'index'));
        $router->addRoute('registration', $registration);


    }  

    /**
     *Get the PDO database connection and set it to the Registry 
     */
    protected function _initDbConfig(){
        $config = new Zend_Config($this->getOptions());
        $params = $config->database->toArray();
        $DB = new Zend_Db_Adapter_Pdo_Mysql($params);    
        $DB->setFetchMode(Zend_Db::FETCH_OBJ);
        Zend_Registry::set('DB',$DB);
    }   

    /**
     *Get the smtp information and set it to the Registry 
     */
    protected function _initSiteConfig(){           
        $registry = Zend_Registry::getInstance();
        $DB = Zend_Registry::get('DB');     
        $selectSiteConfig= "SELECT * FROM site_config order by id"; //fetching available account types  
        $resultSiteInfo = $DB->fetchAssoc($selectSiteConfig);
        $site_config=array();
        foreach($resultSiteInfo as $row){
            $site_config[$row['config_name']]=$row['config_value'];         
        }   
        Zend_Registry::set('site_config',$site_config);
    }   

    /**
     * Setup the Custom Helpers
     */
    protected function _initSetHelper(){
        Zend_Controller_Action_HelperBroker::addPrefix('Helper');
    }   

}

索引.php:

ini_set("display_errors","on");
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));


//Ensure zend library is on include_path   
$rootDir = dirname(__FILE__);
set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path());    

/** Zend_Application */
require_once 'Zend/Application.php';  

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini'
);



$config = new Zend_Config_Ini('application/configs/application.ini','production');  

$siteurl  = $config->siteurl;
$sitedocroot = $config->sitedocroot;


define('SITEURL',$siteurl); //DEFINE SITE URL AS CONSTANT
define('SITEDOCROOT',$sitedocroot); //DEFINE SITE DOC ROOT AS CONSTANT
define('FORMURL',$form_url);
Zend_Registry::set('siteurl',$siteurl); //SET SITE URL IN REGISTRY
Zend_Registry::set('sitedocroot',$sitedocroot); //SET SITE DOC ROOT IN REGISTRY



$currentDate = date("Y-m-d");
define('CURDATE',$currentDate);
Zend_Registry::set('curdate',$currentDate);


$currentDateTime = date("Y-m-d H:i:s");
define('CURDATETIME',$currentDateTime);
Zend_Registry::set('curdatetime',$currentDateTime);


$detailDateForm= date("F j, Y");//March 22 2012
define('DETAILDATE',$detailDateForm);
Zend_Registry::set('detaildate',$detailDateForm);
$application->bootstrap()->run();

我遵循的步骤:

1) 使用 cpanel 创建单独的子域。子域与域具有相同的 DocumentRoot 文件夹。

2)在application.ini文件中编写了下面提到的代码(提到的代码仅适用于图像模块)

resources.router.routes.images.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.images.route = ":module.mysitename.com"
resources.router.routes.images.defaults.module = "images"
resources.router.routes.images.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.images.chains.index.route = ":controller/:action/*"
resources.router.routes.images.chains.index.defaults.controller

我遵循的另一个步骤:

1) 上述步骤中的步骤 1。

2) 在主域的 bootstrap.php 文件(在 _initRoutes 函数上)编写了下面提到的代码

$router = Zend_Controller_Front::getInstance()->getRouter();

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(":images.mysitename.com");

$router->addDefaultRoutes();
foreach ($router->getRoutes() as $key=>$route) {
$router->addRoute('hostname' . $key, $hostnameRoute->chain($route));

我也尝试从其他站点获得帮助,但没有发现任何重要的内容,其中大多数都被提到了控制器。

我的步骤正确吗?请帮助我。

编辑:

根据帖子(David Weinraub 建议),我在 application.ini 上添加了下面提到的代码

resources.modules[] = "images"

; abstract routes to be used in chaining
resources.router.routes.plain.type = "Zend_Controller_Router_Route"
resources.router.routes.plain.abstract = true
resources.router.routes.plain.route = "/:controller/:action"
resources.router.routes.plain.defaults.controller = "index"
resources.router.routes.plain.defaults.action = "index"

resources.router.routes.mysite.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.mysite.abstract = true
resources.router.routes.mysite.route = "images.mysitename.com"
resources.router.routes.mysite.defaults.module = "images"

resources.router.routes.mysite1.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.mysite1.abstract = true
resources.router.routes.mysite1.route = "blog.mysitename.com"
resources.router.routes.mysite1.defaults.module = "blog"

; now the actual (non-abstract) routes are chains of the abstract ones
resources.router.routes.mysite-plain.type = "Zend_Controller_Router_Route_Chain"
resources.router.routes.mysite-plain.chain = "mysite,plain"

resources.router.routes.mysite1-plain.type = "Zend_Controller_Router_Route_Chain"
resources.router.routes.mysite1-plain.chain = "mysite1,plain"

但是,这并没有解决。我收到下面提到的错误。

Warning: is_readable() [function.is-readable]: open_basedir restriction in effect. File(/home/debcho/public_html/application/../../Library/ZendX/Application/Resource/Frontcontroller.php) is not within the allowed path(s): (/home/debcho/public_html:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/debcho/public_html/library/Zend/Loader.php on line 186

Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'Session must be started before any output has been sent to the browser; output started in /home/debcho/public_html/library/Zend/Loader.php/186' in /home/debcho/public_html/library/Zend/Session.php:451 Stack trace: #0 /home/debcho/public_html/library/Zend/Session/Namespace.php(143): Zend_Session::start(true) #1 /home/debcho/public_html/application/Bootstrap.php(128): Zend_Session_Namespace->__construct('Zend_Auth_LipyU...') #2 /home/debcho/public_html/library/Zend/Application/Bootstrap/BootstrapAbstract.php(669): Bootstrap->_initSetUserAccessPermission() #3 /home/debcho/public_html/library/Zend/Application/Bootstrap/BootstrapAbstract.php(622): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('setuseraccesspe...') #4 /home/debcho/public_html/library/Zend/Application/Bootstrap/BootstrapAbstract.php(586): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NULL) #5 /home/debcho/public_html/library/Zend/Application.php(355 in /home/debcho/public_html/library/Zend/Session.php on line 451
4

1 回答 1

1

下面提到的步骤对我有用。

我们需要在 Bootstrap.php 中为子域路径添加一个函数 _initSubDomainRoute()。下面提到的功能成功运行。

public function _initSubDomainRoute()
{
   $pathRoute = new Zend_Controller_Router_Route(':controller/:action/*', array('controller'=>'index', 'action'=>'index'));

   $frontController = Zend_Controller_Front::getInstance();
   $router = $frontController->getRouter();

   $imageDomainRoute = new Zend_Controller_Router_Route_Hostname(
                         'image.examples.com',
                          array(
                            'module'     => 'myimage'
                          ));

   $router->addRoute('imagesdomain', $imageDomainRoute->chain($pathRoute));

   $i_pathRoute_1 = new Zend_Controller_Router_Route(':value', array('controller'=>'Details', 'action'=>'index'));
   $router->addRoute('imagesdomain1', $imageDomainRoute->chain($i_pathRoute_1));



   $jokeDomainRoute = new Zend_Controller_Router_Route_Hostname(
                        'joke.examples.com',
                         array(
                           'module'     => 'myjoke'
                         ));

   $router->addRoute('jokesdomain', $jokeDomainRoute->chain($pathRoute));


   $j_pathRoute_1 = new Zend_Controller_Router_Route(':value', array('controller'=>'Details', 'action'=>'index'));
   $router->addRoute('jokesdomain1', $jokeDomainRoute->chain($j_pathRoute_1));
}

.htaccess 和 index.php 文件具有相同的(上面提到的)代码。未修改此文件上的任何内容以进行子域配置。

于 2013-07-27T05:34:10.097 回答