Zend框架主机名路由与子目录


Zend Framework hostname route with subdir

我的项目模块应该有它自己的域,所以我为它创建了一个路由:

$portalurl = str_replace( 'http://', '', $config->domains->portal );
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
    $portalurl,
    array( 'module' => 'portal' )
);
$defaultroute = new Zend_Controller_Router_Route(
    ':controller/:action',
    array(
        'controller' => 'index',
        'action' => 'index'
    )
);
$contentroute = new Zend_Controller_Router_Route(
    'page/:page',
    array(
        'controller'=> 'content',
        'action' => 'show'
    )
);
$router->addRoute( 'portalDefault', $hostnameRoute->chain($defaultroute) );
$router->addRoute( 'portalContent', $hostnameRoute->chain($contentroute) );

在我的测试系统中,我的应用程序位于/project/public之类的子目录中,当我通过我在系统主机列表中输入的域打开模块时,它可以正常工作。domain.lan/项目/公众。现在我想通过系统(重定向)组装一个url,它组装它没有子目录。

$this->_redirect(
    $this->view->url(array(
        'action' => 'index',
        'controller' => 'index')
    ),
    array( 'prependBase' => false )
); 

是的,我知道最简单的方法来解决它,是配置一个vhost,但它困扰我,我不能找到另一个解决方案,允许它的功能没有vhost和手动设置路径的路由。

最好的方法是什么?

有时ZF不能正确检测baseUrl(它不能在所有事情上都很好……)所以我们需要在引导中手动设置它;你应该可以在。ini文件中这样做,但它对我不起作用。

注意,最好将它放在_initView的开头,在其他内容之前:

protected function _initView()
{
    // Set the Base URL (only needed sometimes)
    $front = Zend_Controller_Front::getInstance();
    $front->setBaseUrl( '/myapp/public' );
    //initialize view
    $view = new Zend_View...

然后使用baseUrl()来包装您的链接文件等:$this->baseUrl('css/template.css');

你必须在你的frontController配置中指定你的"子目录"(在你的application.ini中),它会自动添加到生成的url中。

resources.frontController.baseUrl = "/project/public"