Php 向其他控制器转发请求不起作用-symfony4
我的问题是,将请求转发到另一个控制器不起作用 我已经尝试了symfony文档()中的解决方案/示例 IndexController.phpPhp 向其他控制器转发请求不起作用-symfony4,php,controller,symfony4,Php,Controller,Symfony4,我的问题是,将请求转发到另一个控制器不起作用 我已经尝试了symfony文档()中的解决方案/示例 IndexController.php <?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; class IndexController extend
<?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]",
]);
}
}
<?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;
}
}
OeffnungController.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]",
]);
}
}
<?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;
}
}
我犯了以下错误
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]",
]);
}
}
<?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;
}
}
是否要按参数创建自定义布线?是否在“/”上发送请求并根据此参数将其转发给另一个控制器?