Reputation: 11
No route found for "GET http://pogodynka.localhost:46530/weather"
This is the error I get when I try to access:
http://pogodynka.localhost:46530/weather
config/Routes.yaml
weather_in_city:
path: /weather/{country}/{cityName}
controller: App\Controller\WeatherController:cityAction
requirements:
city: \d+
config/packages/Routing.yaml
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
when@prod:
framework:
router:
strict_requirements: null
src/Controller/WeatherController.php
namespace App\Controller;
use App\Entity\Location;
use App\Repository\MeasurementRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class WeatherController extends AbstractController
{
public function cityAction($cityName, MeasurementRepository $measurementRepository, CityRepository $cityRepository): Response
{
$cities = $cityRepository->findCityByName($cityName);
$city = $cities[0];
$measurements = $measurementRepository->findByLocation($cityName);
return $this->render('weather/city.html.twig', [
'location' => $city,
'measurements' => $measurements,
]);
}
}
Upvotes: 1
Views: 2943
Reputation: 176
You should use route annotations. This way you can define the route directly in the controller class. Here is a nice write up and example: SymfonyCasts: Annotation & Wildcard Routes
My personal opinion but I find annotations to be the way to go.
Upvotes: 1