I am starting to play with symfony4. I've just created new application and create new LuckyController. It works with routes.yaml configured in this manner:
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::number
With the following controller:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
But I want to use annotations. So I decided to comment routes.yaml. Following documentation that explain how to create a route in symfony I've made this:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController extends Controller
{
/**
* @Route("/lucky/number")
*/
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
In Symfony4 you have to install annotations
bundle.
Run this command composer require annotations
Then restart Your project.
Sometimes this problem occurs because of cache.you need to run php bin/console cache:clear
.then it will work fine.
I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL. So the documentation says:
http://localhost:8000/lucky/number will work exactly like before
That did not work. This however did:
http://localhost:8000/en/lucky/number
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With