0

Like in Codeigniter we do have 'core' folder where we can define our own controller like 'MY_Controller' and can be used to extend all the class to extend from this controller is there any possibility to do so in Symfony2.

In symfony I want to create class 'MY_Controller' which extends from the base class 'Controller', and I want all the classes in the controllers to extend from MY_Controller' class.

Thanks in Advance...

2 Answers 2

1

Note:
When working with Symfony2 it is strongly recommended you follow the Symfony2 coding style. It's basically the same as PHP-FIG, with one or two deviations. So underscores are a no-no in class names. Other than that: Symfony is pretty easy to work with, and fully OO, so changing the class a controller extends from is as simple as replacing extends Controller with extends AnotherClass.
But now, the symfony2-way of using a custom controller:

What you could do, is create a Core bundle (CoreBundle henceforth). Then, in this CoreBundle, define a controller, that extends from the Symfony Controller component. From the command line, in your project root, use this command:

php app/console generate:bundle --namespace=YourNameSpace/CoreBundle --bundle-name=YourNameSpaceCoreBundle

More options can be found here
After that, you'll find a DefaultController class in the bundle directories. (probably in the folder src/YourNamespace/CoreBundle/Controller). Then, set about generating your Core controller:

php app/console generate:controller --controller=YourNameSpaceCoreBundle:Core

See the documentation for more options on how to generate your core controller.

After you've finished setting up your custom controller, you can use it in any of the other bundles at will:

namespace YourNameSpace\AnotherBundle\Controller;

use YourNameSpace\CoreBundle\Controller\CoreController;

class DefaultController extends CoreController
{//extends from your custom controller
}

And that's it: you're done.

Sign up to request clarification or add additional context in comments.

Comments

0
  • First, don't name the class using underscores as in PSR-0 each underscore char is converted to a directory separator, when used in class name.
  • Second, put your controllers to <bundledir>/Controller/
  • Third, name your controller something like BaseController and extend all other controllers from it.
  • Fourth, think of using dependency injection rather than coupling functionality in a base controller.

7 Comments

Second point: just use the Symfony2 generate:* commands, controllers will be put in the directories they belong. Third point: That's what the OP is asking: how to do that. fourth point: his own controller will extend from the Symfony2 Controller component. Don't advise him to set about reinventing the wheel: the controller will have access to the container object
Well, yes, it is possible to use generate:*, but personally, I found it slower, then creating a file myself in my IDE... This feature is mainly for newbies, I guess.
Perhaps, but then you don't know if the core controller of the OP is going to require some core services, resources and other objects. Setting up an entire bundle mannually isn't as quick as generating one using the generate:bundle command, though
Hm... I didn't get from the question that the questioner doesn't have a bundle. If it's the point, then for sure, generating a bundle is much easier using the generate:bundle. The question was strictly about extending controllers, not about how to create a bundle ;) But who knows, maybe you guessed the root of the problem ;)
It's not about him having a bundle or not. If he wants all of his controllers to extend from a custom base controller, then that custom base controller should be treated as an abstract component, and thus does not belong in a bundle that does actual work. That's why, in my answer, I advise the OP to generate a Core bundle, put everything in there, and extend those core objects in the actual working bundles
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.