我有问题,将请求转发到另一个控制器不起作用。
我已经尝试过symfony文档(https://symfony.com/doc/current/controller/forwarding.html)中的解决方案/示例。
IndexController.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
<?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;
}
}
?>
我遇到以下错误
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"?
答案 0 :(得分:0)
只需从$thisname
方法中删除未使用的OeffnungController::show
参数并填充注释。它对我有用。
IndexController.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
<?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;
}
}