I get 404 Not Found for all request that are not /. Hitting smfony.test in the browsers opens the web page without a problem. But anything other than symfony.test will result in a 404 Not Found. I looked up several stack overflow solutions (Issue 1, Issue 2), but I was not able to locate the exact problem. Seems like it has to be a misconfiguration from my side, because when I run debug:router I get all URLs:
jernej@blackbook:/var/www/html/symfart$ php bin/console debug:router
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
app_article_index GET ANY ANY /
app_article_save GET ANY ANY /article/save
app_article_hello GET ANY ANY /hello
------------------- -------- -------- ------ --------------------------
Inside my project (/dir path /var/www/project/public) I also have the .htaccess file:
<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>
Since I do not have a lot of experience with Apache server, I rather point out I also did the following change in the Apache configuration (/etc/apache2/sites-enabled/000-default.conf) to add project to url:
<VirtualHost *:80>
ServerName symfony.test
DocumentRoot /var/www/html/project/public
</VirtualHost>
I also added the URL to hosts file in linux. And here is the Controller class:
<?php
namespace AppController;
use AppEntityArticle;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerAbstractController;
class ArticleController extends AbstractController {
/**
* @Route("/", methods={"GET"})
*/
public function index() {
$articles = array();
return $this->render('articles/index.html.twig', array('articles' => $articles));
}
/**
* @Route("/article/save", methods={"GET"})
*/
public function save() {
$entityManager = $this->getDoctrine()->getManager();
$article = new Article();
$article->setTitle('Article One');
$article->setBody('This is the body for article one');
$entityManager->persist($article);
$entityManager->flush();
return new Response('Saved an article with the id of ' + $article->getId());
}
/**
* @Route("/hello", methods={"GET"})
*/
public function hello() {
$articles = array();
return $this->render('articles/index.html.twig', array('articles' => $articles));
}
}
?>
2
Answers
Does your homepage work? If it doesn’t try to change the default hosting path directory for your website from your server.
In this case, you need to allow apache2 to execute .htaccess files from the web root directory…
And then, you need to enable mod_rewrite. You can find this mod in your .htaccess and it’s responsible for URL modifications…