Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On unmatchable routes, AsseticModule has no method of rendering a default or on_error style sheet. #102

Open
mrferos opened this issue Jan 15, 2014 · 1 comment
Labels

Comments

@mrferos
Copy link

mrferos commented Jan 15, 2014

Regular 404s where a route is matched but there is no corresponding method to handle the route work just fine.

The case where a route can not be matched are not handled however, like:

http://site.com/this/route/will/not/match.jpg

I was able to handle this case by sort of re-implementing the renderAssets method in AsseticBundle/Listener.php but perhaps there should be some sort of configuration option to handle this case. My fix for the issue is below:

Application/Module.php

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleDispatchError'), 32);
    }

    public function handleDispatchError(MvcEvent $e)
    {
        $sm = $e->getApplication()->getServiceManager();
        if ($e->getName() != MvcEvent::EVENT_DISPATCH_ERROR)
            return;

        $config = $sm->get('AsseticConfiguration');
        if ($e->getName() === MvcEvent::EVENT_DISPATCH_ERROR) {
            $error = $e->getError();
            if ($error && !in_array($error, $config->getAcceptableErrors())) {
                // break if not an acceptable error
                return;
            }
        }

        $response = $e->getResponse();
        if (!$response) {
            $response = new Response();
            $e->setResponse($response);
        }

        $routeMatch = $e->getRouteMatch();
        if ($routeMatch)
            return;

        /** @var $asseticService \AsseticBundle\Service */
        $asseticService = $sm->get('AsseticService');

        $asseticService->setRouteName('error_404');
        $asseticService->setControllerName('Application\Controller\Index');
        $asseticService->setActionName('error_' . uniqid());

        // Create all objects
        $asseticService->build();

        // Init assets for modules
        $asseticService->setupRenderer($sm->get('ViewRenderer'));

    }
}

Snippet from module.config.php

return array(
    'assetic_configuration' => array(
        'debug' => true,
        'buildOnRequest' => true,
        'webPath' => __DIR__ . '/../../../public/assets',
        'basePath' => 'assets',
        'routes' => array(
            'error_404' => array(
                '@base_css',
                '@head_home_js'
            ),
      )
);
@widmogrod
Copy link
Owner

Did you notice any issues after "reordering"?

@widmogrod widmogrod added the todo label Apr 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants