Reputation: 85
I have some troubles with the framework symfony. I use KernelRequest to manage with my routes. In a controller, I have the right route name and the right params; but in another controller (which is exactly the same, just the page changes) the route generates params like http, https, scheme, etc (see below).
Candidate.php
<?php
namespace AppBundle\Controller;
use AppBundle\AppBundle;
use AppBundle\Entity\CandidateAction;
use AppBundle\Entity\CandidateNotice;
use AppBundle\Entity\CandidateSchool;
use AppBundle\Entity\CandidateSkill;
use AppBundle\Entity\Document;
use AppBundle\Entity\ListElement;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\Query;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Candidate;
use AppBundle\Form\CandidateType;
use AppBundle\Utils\EnumRight;
use AppBundle\Utils\EventCalendar;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use MBence\OpenTBSBundle\OpenTBSBundle;
/**
* Candidat controller.
*
* @Route("/candidate")
*/
class CandidateController extends Controller
{
/**
* Lists all Candidat entities.
*
* @Route("/", name="candidate_index")
* @Method("GET")
*/
public function indexAction(Request $request)
{
$session = $request->getSession();
$session->set('tabActive', 'personalData');
$userHistory = $session->get("userContext")->getUserHistory();
$user = $this->get('security.token_storage')->getToken()->getUser();
if ($session->get("userContext")->hasPermission($user, EnumRight::candidate, EnumRight::candidate_show_list)) {
return $this->render('AppBundle:candidate:index.html.twig', array());
} else {
$this->get("session")->getFlashBag()->add('danger', $this->get('translator')->trans('rights.forbidden'));
$userHistory->popFromHistory();
return $this->redirect($this->generateUrl($userHistory->getPreviousRoute()['name'], $userHistory->getPreviousRoute()['params']));
}
}
}
tender.php
<?php
namespace AppBundle\Controller;
use AppBundle\AppBundle;
use AppBundle\Entity\Document;
use AppBundle\Entity\ListElement;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\Query;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Tender;
use AppBundle\Form\TenderType;
use AppBundle\Utils\EnumRight;
use AppBundle\Utils\EventCalendar;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use MBence\OpenTBSBundle\OpenTBSBundle;
/**
* Tender controller.
*
* @Route("/tender")
*/
class TenderController extends Controller
{
/**
* Lists all Tender entities.
*
* @Route("/", name="tender_index")
* @Method("GET")
*/
public function indexAction(Request $request)
{
$session = $request->getSession();
$session->set('tabActive', 'personalData');
$userHistory = $session->get("userContext")->getUserHistory();
$user = $this->get('security.token_storage')->getToken()->getUser();
if ($session->get("userContext")->hasPermission($user, EnumRight::tender, EnumRight::tender_show_list)) {
return $this->render('AppBundle:tender:index.html.twig', array());
} else {
$this->get("session")->getFlashBag()->add('danger', $this->get('translator')->trans('rights.forbidden'));
$userHistory->popFromHistory();
return $this->redirect($this->generateUrl($userHistory->getPreviousRoute()['name'], $userHistory->getPreviousRoute()['params']));
}
}
}
As you can see, it's exactly the same function in two different controllers.
The route for the first one : (with a dump in my html view)
"name" => "candidate_index"
"params" => []
The route for the second one :
"name" => "tender_index"
"params" => array:5 [▼
"path" => "/tender/"
"permanent" => true
"scheme" => null
"httpPort" => 80
"httpsPort" => 443
]
Here is the result of this command : php/bin console debug:router for both tender and candidate:
$ php bin/console debug:router tender_index
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | tender_index |
| Path | /tender/ |
| Path Regex | #^/tender/$#s |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AppBundle:Tender:index |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
$ php bin/console debug:router candidate_index
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | candidate_index |
| Path | /candidate/ |
| Path Regex | #^/candidate/$#s |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AppBundle:Candidate:index |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
Your assistance will be greatly appreciated.
EDIT : I've found the difference in the log file. This controller is called :
[2016-08-26 10:47:52] request.INFO: Matched route "tender_index". {"route_parameters":{"_controller":"Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction","path":"/tender/","permanent":true,"scheme":null,"httpPort":80,"httpsPort":443,"_route":"tender_index"},"request_uri":"http://capfivm403.capfigroup.domain.com/~mlerouzic/weberp.dev/web/app_dev.php/tender"} []
instead of :
[2016-08-26 10:52:56] request.INFO: Matched route "tender_index". {"route_parameters":{"_controller":"AppBundle\\Controller\\TenderController::indexAction","_route":"tender_index"},"request_uri":"http://capfivm403.capfigroup.domain.com/~mlerouzic/weberp.dev/web/app_dev.php/tender/?httpPort=80&httpsPort=443&path=%2Ftender%2F&permanent=1"} []
Upvotes: 1
Views: 91
Reputation: 85
I have rewritten the name of the main route in my Tender Controller and ... it works... I don't really know why. Maybe a bug in the framework after a composer update in my console.
Upvotes: 0