skip to Main Content

I am trying out Symfony the first time. I installed it with

composer create-project symfony/website-skeleton crudapp

I have an Apache config like this:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "c:/.../Symfony/crudapp/public/"
    <Directory  "c:/.../Symfony/crudapp/public/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

I got a welcome page for http://localhost, but when I try to add a new controller e.g.

php bin/console make:controller
 -> Main

Then the controller is created in the src/Controller folder, but the route is not used by Symfony.

<?php
namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

class MainController extends AbstractController
{
    #[Route('/main', name: 'app_main')]
    public function index(): Response
    {
        return $this->render('main/index.html.twig', [
            'controller_name' => 'MainController',
        ]);
    }
}

Or at least I got 404 error when trying to visit the http://localhost/main.

Any idea how to fix this?

2

Answers


  1. Chosen as BEST ANSWER

    The problem that there was not .htaccess file in the public folder to redirect every request to the index.php. The fix is adding FallbackResource /index.php to the apache configuration. https://symfony.com/doc/6.4/setup/web_server_configuration.html#apache


  2. If this is your first time trying Symfony, I strongly recommend following the documentation on downloading and creating a new project. This includes using the symfony binary to create the project.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search