How to css('@auto') and js('@auto') with routing?

I’m making use of <?= css('@auto') ?> and <?= js('@auto') ?> in my templates. Using routes I’m doing the following:

c::set('routes', array(
    [
        'pattern' => 'application/(:any)',
        'action'  => function($segment_1) {
            return page('application/'. $segment_1);
        },
        'method' => 'GET|POST',
    ],
));

That loads the templates just fine but not the css(’@auto’) and js(’@auto’). Is there a work around for this issue?

You could use the same “trick” as with multi-language sites, i.e. activate the page first:

c::set('routes', array(
    [
        'pattern' => 'application/(:any)',
        'action'  => function($segment_1) {
          
          site()->visit('application/' . $segment_1);
          return page('application/'. $segment_1);
        },
        'method' => 'GET|POST',
    ],
));

@texnixe Sorry, I forgot to reply! Your solution of course worked like a charm (as usual). Can you explain what site()->visit('application/' . $segment_1); is doing behind the scenes? Thanks so much!

Turns out you might as well just

return site()->visit('application/' . $segment_1);

According to the comments, visit() sets the currently active page and returns its page object. The difference is if you check $page->isActive() when using the page helper, it returns false; while it returns true using $site->visit(). But don’t ask me why.

I guess @lukasbestle can enlighten us :heart_eyes:

1 Like

$site->visit() stores the page object as $site->page. This object is then used to get the current page in several places.
First thought: It might be a bug that routes don’t automatically visit the returned pages before rendering the page. However it might also be that this can’t be done for some reason. :slight_smile:

1 Like