转发请求到另一个控制器不起作用 - symfony4
Forward Requests to another Controller does not work - symfony4
我遇到了问题,将请求转发到另一个控制器不起作用。
我试过 symfony 文档中的 solution/example (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"?
刚刚从 OeffnungController::show
方法中删除了未使用的 $thisname
参数并填充了注释。它对我有用。
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;
}
}
我遇到了问题,将请求转发到另一个控制器不起作用。
我试过 symfony 文档中的 solution/example (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"?
刚刚从 OeffnungController::show
方法中删除了未使用的 $thisname
参数并填充了注释。它对我有用。
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;
}
}