1
votes

To the point:
- I created new project on symfony 4.0: composer create-project symfony/skeleton sf4
- Welcome page is working
- I wanted to create my first page with https://symfony.com/doc/current/page_creation.html and I did exactly what they say: create controller and route
- Welcome page changed status to: No route found for "GET /" <-Solved
- new page path can't find file - Error 404

What am I doing wrong? That is silly

~src/Controller/SecurityController.php

<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class SecurityController {
    public function logowanie() {
        return new Response(
            '<html><body>Logowanie</body></html>'
        );
    }
}


~config/routes.yaml

index:
    path: /
    controller: App\Controller\MainController::index

logowanie:
    path: /logowanie
    controller: App\Controller\SecurityController::logowanie

EDIT:
Home page works - thanks @Cerad

3
S4 is kind of sneaky. Out of the box it has no routes defined and defaults to a home page. Once you defined a route then it no longer defaults. Caught me as well the first time I installed it. So do what the error message is saying and add a route for / as well as a controller action. And off topic but do your self a favor and call it SecurityControllerCerad
@Cerad have you any idea for second problem? 404 on /logowanie path. If we trust documentation then it should works.Invictus
bin/console debug:router and then check your spelling very carefully. I copied/pasted in your code and it works as expected. I'm assuming you have not fooled around with services.yaml or any of the other config files.Cerad
You won't see /public anywhere. The trailing slash /logowanie/ might be causing you problems tough I don't see why it is there based on the route you posted.Cerad

3 Answers

6
votes

I had the same problem. Homepage was working, but any other urls generate a 404, served by apache, and not logged in symfony.

My mistake was the missconfiguration of the virtualhost. As there is no longer .htaccess in the public directory, you have to configure redirection & rewrite in your virtualhost. Here is the documentation: https://symfony.com/doc/master/setup/web_server_configuration.html

1
votes

enter image description here

There's no real route nor controller with welcome template on Symfony 4 apps, it's just a trick to improve the user's first experience.

The welcome response work only for root path / and upon added a route or installed a bundle that adds one (e.g. TwigBundle) this response disappears, as well as if debug mode has been deactivated.

Even, the status code of this response still is 404 (NOT FOUND).

1
votes

This code is the solution in Symfony 4.1:

composer require symfony/apache-pack

After the link can be used:

http://localhost/(name-project)/public/index.php/(name of controller)