首页 文章

路由不在Symfony 3.4中工作

提问于
浏览
2

我使用以下方法创建了一个新的Symfony 3.4项目:

composer create-project symfony/skeleton my-project

之后我添加了以下组件:

composer require twig
composer require annotations
composer require maker

并创建了一个控制器:

php bin/console make:controller

我添加了一个“合法”路线的动作 . 这是DefaultController:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function index()
    {
        return $this->render('index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }

    /**
     * @Route("/legal", name="legal")
     */
    public function legal()
    {
        return $this->render('legal.html.twig', []);
    }
}

文件config / routes.yaml:

#index:
#    path: /
#    defaults: { _controller: 'App\Controller\DefaultController::index' }

和config / routes / annotations.yaml:

controllers:
    resource: ../../src/Controller/
    type: annotation

当我访问主页时没问题,页面正在显示 . 但是当我尝试/ legal页面时,我有一个404:

未找到 - 在此服务器上找不到请求的URL / legal .

php bin/console debug:router 显示预期:

------------------ -------- -------- ------ -------------------------- 
  Name               Method   Scheme   Host   Path                      
 ------------------ -------- -------- ------ -------------------------- 
  homepage           ANY      ANY      ANY    /                         
  legal              ANY      ANY      ANY    /legal                    
  _twig_error_test   ANY      ANY      ANY    /_error/{code}.{_format}  
 ------------------ -------- -------- ------ --------------------------

我使用console命令清除了缓存,并删除了var / cache目录的内容 . 但仍然是404 .

我是3.4的新手 . 有任何想法吗 ?谢谢...

3 回答

  • 5

    你是否在AppKernel.php中加载了Bundle“App”?有一个数组你在哪里打开添加你的Bundle的AppBundle.php .

    $bundles = [
    /**lot of other bundles*//
    new App\AppBundle() 
    ]
    

    它看起来像这样

  • 3

    好吧,正如@Basel Issmail所指出的那样, Symfony/Flex 并没有像以前的Symfony安装程序那样创建一个 .htaccess ,而我忘记了它 .

    我只有一个最小的Apache配置文件:

    <VirtualHost *:80>
        DocumentRoot /path/to/my-project/public
        ServerName myproject.localhost
    
        <Directory /path/to/my-project/public>
            Options -Indexes +FollowSymLinks -MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

    所以我在 /public/index.php 文件所在的位置)中创建了 .htaccess 文件,所需的最小配置如下:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Determine the RewriteBase automatically and set it as environment variable.
        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]
    
        # If the requested filename exists, simply serve it.
        # We only want to let Apache serve files and not directories.
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]
    
        # Rewrite all other queries to the front controller.
        RewriteRule .? %{ENV:BASE}/index.php [L]
    </IfModule>
    

    缺少的部分是将所有查询(除资产等现有文件除外)重写为 index.php ,以便Symfony处理路由 .

  • 0

    听起来你需要设置 RewriteBaseapache ,因为问题不在/

    因为你使用的是Symfony / flex,所以甚至可能缺少 .htaccess 文件,并且它不像以前的Symfony版本那样默认添加它 .

    查看此页面至configure a Web Server

    .htaccess 应位于公共文件夹中,并且在Apache下运行应用程序的最低配置是:

    <VirtualHost *:80>
        ServerName domain.tld
        ServerAlias www.domain.tld
    
        DocumentRoot /var/www/project/public
        <Directory /var/www/project/public>
            AllowOverride All
            Order Allow,Deny
            Allow from All
        </Directory>
    
        # uncomment the following lines if you install assets as symlinks
        # or run into problems when compiling LESS/Sass/CoffeeScript assets
        # <Directory /var/www/project>
        #     Options FollowSymlinks
        # </Directory>
    
        ErrorLog /var/log/apache2/project_error.log
        CustomLog /var/log/apache2/project_access.log combined
    </VirtualHost>
    

相关问题