开发者

Zend Framework hostname route with subdir

A module of my project should have it's own domain so i created a route for it:

$portalurl = str_replace( 'http://', '', $config->开发者_C百科;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) );

In my test system my application is in a sub directory like /project/public which works fine when i open the module via the domain i entered in my system host list. domain.lan/project/public. Now i want to assemble a url via the system (redirect) and it assebles it without the sub directory.

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

Yes i know the easiest way to solve it, is to configure a vhost but it bothers me that i can't find another solution wich allows it to function without a vhost and setting the path manually in the routes.

What is the best approach for this?


Sometimes ZF cannot properly detect the baseUrl (it can't be great at everything...) so we need to set it manually in the bootstrap; you should be able to do this in the .ini, but it did not work for me.

Note that it is best to put this right at the beginning of the _initView, before anything else:

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

Then use baseUrl() to wrap your linked files, etc.: $this->baseUrl('css/template.css');


You have to specify you "subdirectory" in your frontController config (in your application.ini), it will automatically be prepended to generated urls.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜