Symfony -找不到GET的路由

ngynwnxp  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(182)

找不到“GET http://pogodynka.localhost:46530/weather"”的路由
这是我尝试访问以下内容时遇到的错误:

http://pogodynka.localhost:46530/weather

配置/路由.yaml

weather_in_city:
  path: /weather/{country}/{cityName}
  controller: App\Controller\WeatherController:cityAction
  requirements:
    city: \d+

配置/包/路由.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

源代码/控制器/天气控制器.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,
        ]);
    }
}

debug:router

7gcisfzg

7gcisfzg1#

你应该使用路由注解。这样你就可以直接在控制器类中定义路由。下面是一个很好的例子:SymfonyCasts: Annotation & Wildcard Routes
我个人的意见,但我发现注解是要走的路。

相关问题