Static assets not refreshing with symfony2 clear cache command

前端 未结 6 825
一个人的身影
一个人的身影 2021-02-13 17:12

I\'m using Assetic with the compass filter to pass and compile .scss files. This part of the setup seems to work fine. However, my understanding was that in the app_dev environm

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 17:55

    I know this is an old topic but the only answers I could fine were the CompassElephantBundle and the above AsseticController hack. I've got an approach which is essentially but means I didn't have to edit the vendor package.

    The way I have done it is to edit copy the original AsseticController and then linking to that in the config from the parameters.

    parameters:
        assetic.controller.class: Acme\RandomBundle\Controller\AsseticController
    

    The copied AsseticController just does a preg_match for the filetype from the source path and corrects caching from there.

    enableProfiler && null !== $this->profiler) {
            $this->profiler->disable();
        }
    
        if (!$this->am->has($name)) {
            throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
        }
    
        $asset = $this->am->get($name);
        if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
            throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
        }
    
        $bustCache = preg_match('/\.(scss|sass|less)$/', $asset->getSourcePath());
    
        $response = $this->createResponse();
        $response->setExpires(new \DateTime());
    
        if ($bustCache) {
            $lastModified = time();
            $date = new \DateTime();
            $date->setTimestamp($lastModified);
            $response->setLastModified($date);
        }
        else
        {
            // last-modified
            if (null !== $lastModified = $asset->getLastModified()) {
                $date = new \DateTime();
                $date->setTimestamp($lastModified);
                $response->setLastModified($date);
            }
        }
    
        // etag
        if ($this->am->hasFormula($name)) {
            $formula = $this->am->getFormula($name);
            $formula['last_modified'] = $lastModified;
            $response->setETag(md5(serialize($formula)));
        }
    
        if ($response->isNotModified($this->request)) {
            return $response;
        }
    
        if ($bustCache) {
            $response->setContent($asset->dump());
        }
        else {
            $response->setContent($this->cachifyAsset($asset)->dump());
        }
    
        return $response;
    }
    
    /* Rest of controller */
    

提交回复
热议问题