Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - Check if controller exists

Tags:

php

symfony

My situation: I have a NavigatorController which is triggered by AJAX requests, and will

$this->forward("controllername")

the request. But how can I check if the controller exists based on controller name? Of course, BEFORE the actual forward happens and throws an error when the page controller does not exists.

like image 462
Ralph Avatar asked Jan 29 '23 21:01

Ralph


1 Answers

You can actually use the controller_resolver service that Symfony uses in order to check if controller exists.

public function indexAction(Request $request)
{
    $request->attributes->set('_controller', 'AppBundle\Controller\ExampleController::exampleAction');
    try{
        $this->get('debug.controller_resolver')->getController($request);
    } catch (\Exception $e) {
        $x = $e->getCode();
    }
}

Hope it helps!

like image 179
Keloo Avatar answered Feb 02 '23 08:02

Keloo