Friday 15 June 2012

php - Loading a Parent Class in Symfony 2 -


I have set up a test root class in my Symfony 2 controller as follows:

  & Lt ;? Php namespace Zetcho \ AmColAnBundle \ controller; Use Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; Extends the class base controller controller {public function valid user () {$ user ['first_name'] = "name"; $ User ['signin'] = true; $ User return; }} Class extends the DefaultController BaseController {public function index action () {$ user = $ this-> ValidateUser (); $ DisplayParms ['user'] = $ user; $ $ Return - ('zetkoamcolnbundle: default: index.html.twig', $ displayParms); }}   

The code is in the code src / Zetcho / AmColAnBundle / Controller / DefaultController.php. The test code works now I want to take the parent class (base controller) from my original controller file So that I can reuse it in my other controllers. I want to place it in the same directory as other controllers and I would like to declare it the same way as in the above usage details, what is the best / accepted way to do this in the controller Symfony 2?

You do it in SimFony 2 the same way you would be with any PHP class. Split your classes into different files like this: -

src / Zetcho / AmColAnBundle / Controller / BaseController.FP

  Namespace Zetcho \ AmColAnBundle \ Controller; Use Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; Extends the class base controller controller {public function valid user () {$ user ['first_name'] = "name"; $ User ['signin'] = true; $ User return; }}   

src / Zetcho / AmColAnBundle / Controller / DefaultController.php

  Namespace Zetcho \ AmColAnBundle \ Controller; Use Zetcho \ AmColAnBundle \ Controller \ BaseController; The square spreads to the defaultController BaseController {public function index action () {$ user = $ this- & gt; Valid user (); $ DisplayParms ['user'] = $ user; $ $ Return - ('zetkoamcolnbundle: default: index.html.twig', $ displayParms); }}   

Once you know that really is very simple, remember that the controllers in symfony2 are only normal PHP classes, nothing special about them.

No comments:

Post a Comment