0

I would like to know how to access to a function of a controller from inside another controller in Symfony2. In fact I have two controllers: "EventgroupeController" and "GroupeController". In the code of the controller "EventgroupeController" I put the instruction below:

return GroupeController::AfficheGroupeAction();

But when I run the code (or let's say the project I am developing), it displays this error message in Symfony2:

ContextErrorException: Runtime Notice: Non-static method Ikproj\GroupeBundle\Controller\GroupeController::AfficheGroupeAction() should not be called statically, assuming $this from incompatible context in C:\wamp\www\Wkayet_project\PFESymfony2\src\Ikproj\GroupeBundle\Controller\EventgroupeController.php line 104

After having a look at this link: How to access a different controller from inside a controller Symfony2 in order to know how to access a different controller from inside a controller in Symfony2, I modified the content of the file services.yml as below:

 parameters:
#    ikproj_groupe.example.class: Ikproj\GroupeBundle\Example

services:
#    ikproj_groupe.example:
#        class: %ikproj_groupe.example.class%
#        arguments: [@service_id, "plain_value", %parameter%]
     controllerservice:
         class: Ikproj\GroupeBundle\Controller\GroupeController

Then, I replaced the instruction: return GroupeController::AfficheGroupeAction(); by the lines below:

$yourController = $this->get('controllerservice');
$yourController1 = $yourController::AfficheGroupeAction();
return $yourController1;

But I still see this error message:

ContextErrorException: Runtime Notice: Non-static method Ikproj\GroupeBundle\Controller\GroupeController::AfficheGroupeAction() should not be called statically, assuming $this from incompatible context in C:\wamp\www\Wkayet_project\PFESymfony2\src\Ikproj\GroupeBundle\Controller\EventgroupeController.php line 106

So, my question is: how can I resolve this problem and how can I access to the function AfficheGroupeAction() of the controller "GroupeController" from inside the controller "EventgroupeController"?

Community
  • 1
  • 1
Nadim
  • 382
  • 1
  • 7
  • 29
  • Is this `AfficheGroupeAction` static method ?? Becouse the error says that its not. – kskaradzinski Aug 20 '14 at 11:14
  • No, it isn't a static method, by the way, this is the header of the function AfficheGroupeAction : public function AfficheGroupeAction(){ Do you have any idea about how to call a non-static function?? – Nadim Aug 20 '14 at 11:39
  • Why You want to call it in another controller ?? If You need to do something whats inside this method move it to service, And call it in both places – kskaradzinski Aug 20 '14 at 12:09
  • Well I have to do that because after the validation of a form, a web page of a group will be displayed immediately..So for that reason I have to call a function of a controller inside another controller in Symfony2. – Nadim Aug 21 '14 at 09:26
  • ok..it is done, please have a look at the answer at the bottom of the page – Nadim Aug 21 '14 at 10:29
  • OMG. see instead `return GroupeController::AfficheGroupeAction();` use `forward` method `$this->forward('GrupeController:Aff...');` – kskaradzinski Aug 21 '14 at 10:35
  • @skowron-line: Well, I have to put "return" inside a controller in Symfony2, By the way I tried this instruction: return $this->forward('GroupeController::AfficheGroupeAction()'); but I obtained this error message: Class "GroupeController" does not exist. ... Then what is the correct instruction to put?? – Nadim Aug 21 '14 at 10:59
  • http://symfony.com/doc/current/book/controller.html#forwarding – kskaradzinski Aug 21 '14 at 11:26

1 Answers1

0

An action method must not be static.

$this->get('controllerservice')->youMethod();  

Should work !

But with a good "application design", you should not have this need except if you want to forward a request from a controller to another ( example : backward compatibility ). In this case you can use the forward method provided by symfony2 base controller. ( http://symfony.com/doc/current/book/controller.html )

Fabien MEYNARD
  • 597
  • 4
  • 12
  • Well, I tried this code: return $this->get('controllerservice')->AfficheGroupeAction(); but I obtained this error message: FatalErrorException: Error: Call to a member function get() on a non-object in C:\wamp\www\Wkayet_project\PFESymfony2\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php line 256 ... So as you can notice here, it didn't work!!...By the way, what is the problem here? – Nadim Aug 21 '14 at 09:21
  • Your controller extends Symfony2 Controller class? – Fabien MEYNARD Aug 21 '14 at 09:50
  • yes of course it does, this is its header: class GroupeController extends Controller – Nadim Aug 21 '14 at 10:04
  • would like that I give you all the code of the controller?? – Nadim Aug 21 '14 at 10:12
  • If you want to get help give maximum info :) – Fabien MEYNARD Aug 21 '14 at 10:14
  • ok, I will put all the code of each of the controller "EventgroupeController" , the controller "GroupeController" and the file services.yml in an answer at the bottom. – Nadim Aug 21 '14 at 10:23
  • ok...it is done, please have a look at the answer at the bottom of the page. – Nadim Aug 21 '14 at 10:32