I just started following a symfony 4 tutorial I set up the whole project with composer and got the index file showing up in browser http://127.0.0.1:8000/
I'm stuck on the very first Router/Controller/View example The route is not working properly or at all. I always get a 404 error.
1:
2:
3:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
$number = random_int(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
# the "app_lucky_number" route name is not important yet
app_lucky_number:
path: /lucky/number
controller: App\Controller\LuckyController::number
I'm out of ideas, I've looked everywhere on google and did not found a solution.
Make sure your contorller is on the right path src/Controller/
Example
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController extends AbstractController
{
public function number()
{
echo __LINE__;die();
}
}
Check the config/routes.php
files have the correct routes
namespace Symfony\Component\Routing\Loader\Configurator;
use App\Controller\LuckyController;
return function (RoutingConfigurator $routes) {
// Matches /lucky exactly
$routes->add('lucky_number', '/lucky')
->controller([LuckyController::class, 'number'])
;
};
OR
Change your YAML to
# the "app_lucky_number" route name is not important yet
app_lucky_number:
path: /lucky
controller: App\Controller\LuckyController::number
After doing this access this URL http://127.0.0.1:8000/lucky