简体   繁体   English

将请求转发到另一个 Controller 不起作用 - symfony4

[英]Forward Requests to another Controller does not work - symfony4

I've the problem, that forward Requests to another Controller does not work.我有问题,将请求转发到另一个 Controller 不起作用。

I've tried the solution/example from the symfony documentation ( https://symfony.com/doc/current/controller/forwarding.html ).我已经尝试了 symfony 文档( https://symfony.com/doc/current/controller/forwarding.html )中的解决方案/示例。

IndexController.php索引控制器.php


<?php

namespace App\Controller;
use App\Controller\OeffnungController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class IndexController extends AbstractController
{
    /**
     * @Route("/")
     */
    public function index($thisname)
    {
        $thisname = "TEST";
        return $this->render('index/index.html.twig', [
            'controller_name' => 'IndexController',
            'pagetype' => 'index',
            'pageurl' => "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
        ]);
    }
}

?>

OeffnungController.php OeffnungController.php

<?php

namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Offnungszeiten;
use App\Controller\IndexController;

class OeffnungController extends AbstractController
{
    public function show($thisname)
    {
        $product = $this->getDoctrine()
            ->getRepository(Offnungszeiten::class)
            ->findOneBy(['name' => 'TESTPRODUCT']);

        if (!$product) {
            throw $this->createNotFoundException(
                'No product found for id '
            );
        }

        $response = $this->forward('App\Controller\IndexController::index', [
            'thisname' => $product->getName(),
        ]);
        return $response;

    }
}
?>

I got the following error我收到以下错误

Could not resolve argument $thisname of "App\Controller\IndexController::index()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?

Just removed the unused $thisname argument from the OeffnungController::show method and filled the annotation.刚刚从OeffnungController::show方法中删除了未使用的$thisname参数并填充了注释。 And it works for me.它对我有用。

IndexController.php索引控制器.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class IndexController extends AbstractController
{
    /**
     * @Route("/", name="app_index_index")
     */
    public function index($thisname)
    {
        $thisname = "TEST";

        return $this->render('index/index.html.twig', [
            'controller_name' => 'IndexController',
            'pagetype' => 'index',
            'pageurl' => "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
        ]);
    }
}

OeffnungController.php OeffnungController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Offnungszeiten;

class OeffnungController extends AbstractController
{
    /**
     * @Route("/oeffnung/show", name="app_oeffnung_show")
     */
    public function show()
    {
        $product = $this->getDoctrine()
            ->getRepository(Offnungszeiten::class)
            ->findOneBy(['name' => 'TESTPRODUCT']);

        if (!$product) {
            throw $this->createNotFoundException(
                'No product found for id '
            );
        }

        $response = $this->forward('App\Controller\IndexController::index', [
            'thisname' => 'name',
        ]);

        return $response;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM