Search code examples
phpzend-frameworkzend-controller-router

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


Solution

  • 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');