You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 4公共目录图片触发NotFoundHttpException:路由缺失致图片无法显示

Fixing "No route found for GET /image/seta.png" in Symfony 4

Let’s walk through exactly why you’re hitting this NotFoundHttpException and how to fix it—this is a super common static asset issue in Symfony, so you’re not alone!

Step 1: Verify your image’s location

Symfony serves all static assets (images, CSS, JS) exclusively from the public/ directory at your project root. Double-check that:

  • You have a public/image/ folder (create it if it doesn’t exist)
  • Your seta.png file is directly inside public/image/

If the file lives somewhere else (like src/Resources/ or the project root), Symfony’s web server won’t be able to access it directly.

Step 2: Use the asset() function in your templates

Never hardcode asset paths in Twig—use Symfony’s built-in asset() helper to generate the correct URL. This avoids path mistakes and ensures compatibility with environment-specific setups (like asset versioning in production).

Replace any hardcoded image tags like this:

<img src="/image/seta.png" alt="Seta">

With this Twig code:

<img src="{{ asset('image/seta.png') }}" alt="Seta">

Step 3: Check your server configuration

If you’re using the Symfony built-in server (started with symfony server:start or php -S localhost:8000 -t public), it should handle static assets automatically. But if you’re using Apache or Nginx:

  • Apache: Make sure your virtual host’s DocumentRoot points to the public/ folder, not the project root. Example:
    DocumentRoot "/var/www/your-project/public"
    <Directory "/var/www/your-project/public">
        AllowOverride All
        Require all granted
    </Directory>
    
  • Nginx: Ensure your server block sets root to the public/ directory:
    server {
        root /var/www/your-project/public;
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    }
    

If your server points to the project root instead of public/, it’ll look for image/seta.png in the root (which doesn’t exist) and pass the request to Symfony’s router—causing the "No route found" error.

Step 4: Clear your Symfony cache

Stale cache can cause unexpected asset glitches, especially in development. Run this command in your project root:

php bin/console cache:clear

Then restart your server to ensure changes take effect.

Step 5: Check permissions and custom routes

  • Permissions: Ensure public/image/seta.png has readable permissions (usually 644) and the public/image/ folder has 755 permissions. The web server needs to read the file.
  • Custom Routes: If you’ve defined routes matching patterns like /image/{file}, they might intercept the static asset request. Double-check your config/routes.yaml (or annotation routes) to make sure these aren’t overriding static asset handling.

Test directly

After making these changes, try accessing http://localhost:8000/image/seta.png directly in your browser. If the image loads, your template should display it correctly too.

内容的提问来源于stack exchange,提问作者Ronald2g

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:13:01