Alessio De Zotti
Alessio De Zotti

Reputation: 85

zf2 pagination with optional subdomain

I'm having hard time working with ZF2 routing and pagination. I'm having this configuration for routing:

 'router' => array(
    'routes' => array(

        'website' => array(
            'type' => 'Hostname',
            'options' => array(
                'route' => '[:subdomain]foo.local',
                'constraints' => array(
                    'subdomain' => '[www.|test.]'
                ),
                'defaults' => array(
                    'controller'    => 'Frontend\Controller\Index',
                    'action'        => 'index',
                    'subdomain'     => 'www.'
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(

                'homepage' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Index',
                            'action'     => 'index',
                        ),
                    ),
                ),
                'structures' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/structures[/:action]',
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Structures',
                            'action'     => 'index',
                        ),
                    ),
                ),
                [...]
                'frontend_blog' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/blog[/:page]',
                        'constraints' => array(
                            'page' => '[0-9]+'
                        ),
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Blog',
                            'action'     => 'index',
                        ),
                    ),
                ),
                'frontend_single' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/blog/view/[:id]',
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Blog',
                            'action'     => 'view',
                        ),
                    ),
                ),
                'frontend_bycategory' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/blog/category/[:id]',
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Blog',
                            'action'     => 'category',
                        ),
                    ),
                ),
                'frontend_news' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/news[/:page]',
                        'constraints' => array(
                            'page' => '[0-9]+'
                        ),
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\News',
                            'action'     => 'index',
                        ),
                    ),
                ),
                [...]
            ),
        ),
    ),
),

That is, I want foo.local to be available as www.foo.local, test.foo.local or foo.local: actually it works fine. The hard part comes with pagination view helper: if I use it this way:

 <?php echo $this->paginationControl($this->list, 'Sliding', 'frontend/pagination_control', array('route' => 'website/frontend_blog')); ?>

it always outputs as link foo.local/blog/2, while I'd prefer it to keep the www or test if I'm visiting www.foo.local/blog or test.foo.local/blog. What am I doing wrong?

Maybe useful to explain I have pretty the same config for a mobile routing, that's way I' using a 'website' child routes container. Thx. A.

@Crisp: Here is the content of frontend_paginationcontrol:

 <?php if ($this->pageCount > 1): ?>

<nav id="paginazione">
    <ul>
        <?php if (isset($this->previous)): ?>
            <li><a href="<?php echo $this->url($this->route, array('page' => $this->previous), array(), true); ?>">&#8249;</a></li>
        <?php else: ?>
            <li><a href="#" class="disabilitato">&#8249;</a></li>
        <?php endif; ?>
        <li><a href=""><?php echo $this->current; ?></a></li>
        <li>di</li>
        <li><a href=""><?php echo $this->pageCount ?></a></li>
        <?php if (isset($this->next)): ?>
            <li><a href="<?php echo $this->url($this->route, array('page' => $this->next), array(), true); ?>">&#8250;</a></li>
        <?php else: ?>
            <li><a href="#" class="disabilitato">&#8250;</a></li>
        <?php endif; ?>
    </ul>
</nav>

I tryied as you suggested but does not change the result :(

UPDATE: Actually the problem appears to be caused by setting default value for subdomain, with this configuration it works fine ( this is the module

website' => array(
            'type' => 'Hostname',
            'options' => array(
                'route' => '[:subdomain.]foo.local',
                'constraints' => array(
                    'subdomain' => '(www|test)'
                ),
                'defaults' => array(
                    'controller'    => 'Frontend\Controller\Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(

                'homepage' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            'controller' => 'Frontend\Controller\Index',
                            'action'     => 'index',
                        ),
                    ),
                ),

Upvotes: 2

Views: 212

Answers (1)

Jeremy Giberson
Jeremy Giberson

Reputation: 1143

You set the default for the subdomain route parameter to "www.". Leave it blank, to keep what is currently being used or if you want something else to be used during URL generation, you can to pass that route parameter with your desired value.

$this->url($this->route, array('page' => $this->previous), array(), true); ?>

simply becomes

$this->url($this->route, array('page' => $this->previous, 'subdomain' => 'test'), array(), true); ?>

Upvotes: 0

Related Questions