I have created a new Symfony 3.4 project using:
composer create-project symfony/skeleton my-project
After that I added the following components:
composer require twig
composer require annotations
composer require maker
And created a Controller:
php bin/console make:controller
I added an action with a route "legal". Here is the 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', []);
}
}
File config/routes.yaml:
#index:
# path: /
# defaults: { _controller: 'App\Controller\DefaultController::index' }
And config/routes/annotations.yaml:
controllers:
resource: ../../src/Controller/
type: annotation
When I access the homepage, no problem, the page is showing. But when I try the /legal page, I have a 404 :
Not Found - The requested URL /legal was not found on this server.
php bin/console debug:router
shows the expected:
------------------ -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------ -------- -------- ------ --------------------------
homepage ANY ANY ANY /
legal ANY ANY ANY /legal
_twig_error_test ANY ANY ANY /_error/{code}.{_format}
------------------ -------- -------- ------ --------------------------
I cleared the cache, with the console command and by removing the content of the var/cache directory. But still the 404.
I'm new to 3.4. Any ideas ? Thanks...
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.