I've the problem, that forward Requests to another Controller does not work.
I've tried the solution/example from the symfony documentation ( 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;
}
}
?>
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. And it works for me.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.