如何使用ZEND模块(不是Zend控制器)作为子域

时间:2012-12-01 13:21:40

标签: php .htaccess zend-framework routing routes

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

.htaccess代码:

<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>

bootstrap.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');
    }   

}

的index.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

1 个答案:

答案 0 :(得分: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文件具有相同的(上述)代码。没有修改此文件上的任何内容以进行子域配置。