Routing not working in Symfony 3.4

前端 未结 4 2009
盖世英雄少女心
盖世英雄少女心 2021-01-18 22:03

I have created a new Symfony 3.4 project using:

composer create-project symfony/skeleton my-project

After that I added the following compo

相关标签:
4条回答
  • 2021-01-18 22:31

    Sounds like you need to setup your RewriteBase with apache, since the problem is not at /

    Maybe .htaccess file is even missing since you are using Symfony/flex, and it doesn't add it by default like previous Symfony editions.

    Check this page to configure a Web Server

    .htaccess should be inside public folder and The minimum configuration to get your application running under Apache is:

    <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>
    
    0 讨论(0)
  • 2021-01-18 22:42

    Did you load your Bundle "App" in the AppKernel.php ? There is a Array where you musst Open Add the AppBundle.php of your Bundle.

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

    It musst look Like this

    0 讨论(0)
  • 2021-01-18 22:48

    Well, as @Basel Issmail pointed out, Symfony/Flex doesn't create a .htaccess like the previous Symfony installer did, and I had forgotten it.

    I just had a minimal Apache configuration file:

    <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>
    

    So I created the .htaccess file in /public/ (where the index.php file lies), and the minimal required configuration is something like:

    <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>
    

    The missing part was to rewrite all queries (except existing files like assets) to index.php, for Symfony to handle the route.

    --- Edit ---

    Instead of manually creating the .htaccess, you can also just use a symfony flex recipe:

    composer require apache-pack
    

    This will install this .htacess file.

    0 讨论(0)
  • 2021-01-18 22:48

    Got same issue with symfony install on Synology DSM 6.2. I confirm, just run "composer require apache-pack" as mentioned by scandel, and annotations routing will work great. The .htaccess file with rewrite cond will be automatically created in the "public" folder.

    Thank you ! :)

    Here the .htaccess file created by the symfony flex install :

    #----------------------------
    # Use the front controller as index file. It serves as a fallback solution when
    # every other rewrite/redirect fails (e.g. in an aliased environment without
    # mod_rewrite). Additionally, this reduces the matching process for the
    # start page (path "/") because otherwise Apache will apply the rewriting rules
    # to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
    DirectoryIndex index.php
    
    # By default, Apache does not evaluate symbolic links if you did not enable this
    # feature in your server configuration. Uncomment the following line if you
    # install assets as symlinks or if you experience problems related to symlinks
    # when compiling LESS/Sass/CoffeScript assets.
    # Options FollowSymlinks
    
    # Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
    # to the front controller "/index.php" but be rewritten to "/index.php/index".
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Determine the RewriteBase automatically and set it as environment variable.
        # If you are using Apache aliases to do mass virtual hosting or installed the
        # project in a subdirectory, the base path will be prepended to allow proper
        # resolution of the index.php file and to redirect to the correct URI. It will
        # work in environments without path prefix as well, providing a safe, one-size
        # fits all solution. But as you do not need it in this case, you can comment
        # the following 2 lines to eliminate the overhead.
        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]
    
        # Sets the HTTP_AUTHORIZATION header removed by Apache
        RewriteCond %{HTTP:Authorization} .
        RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect to URI without front controller to prevent duplicate content
        # (with and without `/index.php`). Only do this redirect on the initial
        # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
        # endless redirect loop (request -> rewrite to front controller ->
        # redirect -> request -> ...).
        # So in case you get a "too many redirects" error or you always get redirected
        # to the start page because your Apache does not expose the REDIRECT_STATUS
        # environment variable, you have 2 choices:
        # - disable this feature by commenting the following 2 lines or
        # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
        #   following RewriteCond (best solution)
        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
    
        # 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>
    
    <IfModule !mod_rewrite.c>
        <IfModule mod_alias.c>
            # When mod_rewrite is not available, we instruct a temporary redirect of
            # the start page to the front controller explicitly so that the website
            # and the generated links can still be used.
            RedirectMatch 307 ^/$ /index.php/
            # RedirectTemp cannot be used instead
        </IfModule>
    </IfModule>
    #----------------------------
    
    0 讨论(0)
提交回复
热议问题