Best place for other php.class in symfony2? - symfony

i have a php.class that handle gd-functions in my symfony2-project. Where i can find the best place for this classes?
Regards

Create a folder in src/. To autoload the classes that are in the folder you will create, use Composer Autoload feature (https://getcomposer.org/doc/04-schema.md#classmap) or autoload it in /app/ (https://github.com/symfony/symfony-standard/blob/2.7/app/autoload.php)

If your Class is also useable without an Symfony2 context you should not place it in Acme\Bundles\DemoBundle but rater under Acme\Components\ChangeThisToWhateverYourLibsNameIs. Then you can define an service in your bundle for this class to use this as an service in an Symfony context.
Why?
Because with this way you can reuse the library in that namespace in other frameworks two. You could maybe create a new composer package for it and add it as an dependency of your bundle.
As you see the Symfony core team does it the same way:
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Add it to any of your bundles. You sure must have bundles created. Add a directory to your bundle. The directory can be named anything. I normally name it Services. Add namespace to your class. Lets say if your bundle name is AcmeDemoBundle and your directory name is Services, your namespace would be Acme\Bundles\DemoBundles\Services
You can then register this class as a service and access via DI.

Related

Can Symfony Bundle be used in non-Symfony project (f.e. Zend)?

Can I reuse my Bundle created in Symfony in non-Symfony project, f.e. Zend? (Or I only can reuse Components?)
What about services from that Bundle?
A Symfony bundle can certainly be included in with other projects, via a composer.json & composer.lock file - it doesn't mean that there is any useful code to run within that bundle however.
If there is useful code as part of the bundle, then you can use it directly, but a Symfony Bundle is just a library that will usually include some Symfony-specific configuration.
Best practice for a bundle is to put any useful, common code, into a separate library (which could be used independently - such as what Symfony calls a 'Component'), and then enable that code (for example creating Symfony services, or configuration) with the bundle configuration.
There have been projects that are Symfony bundles, and also have the configurations for other frameworks as well, such as Silex, and also appropriate Laravel configurations within the same codebase.

How to use variables in Symfony Doctrine ORM annotations?

In short, I would like to use something like:
#ORM\Table(name="schemaname.tablename")
but replacing the "schemaname" string with a variable, that can be set as a configuration parameter somewhere (like in parameters.yml file)
I understand your context as a reusable bundle entity with cross domain relation to an other bundle.
I don't find anything about doctrine mapping customisation, but as your bundle can only be included once per project, I recommand you to use your bundle name as a prefix for the table.
Like yourapp_tablename
For the crossdomain constraint, your bundle, if it is reusable can't have dependencies to external bundles. It is your business bundles which have to use the reusable bundle, not the way around. I guess you have to use interfaces if you want an external bundle to be extending your model class

Where shall I put my business logic without mapping to a database in Symfony2?

I created a bundle in Symfony2 that contains my webapp. In there I have some Entities that are mapped to my MySQL-Database. They are located in Acme\MyBundle\Entities.
Now I have created some helper classes that I will not just use in MyBundle, so I outsourced them into a separate bundle (e.g. Acme\MyHelperBundle). Everything is clear until here.
But where exactly do I put business logic files? Do I put them into
Acme\MyHelperBundle\Entities or
Acme\MyHelperBundle\Resources or
Acme\MyHelperBundle\Resources\src
or any other location?
And what would be a good namespace for these helper classes?
create a model folder and in that make a class where you put the business logic and create a service of that class and used that classes anywhere in the project.
I think there's no real rules for that.
if your helper class are declared as services (i think so if you want to use them in all your bundle) you can create a Manager folder. So when i'm on a bundle and there is a Manager, i know that i have some logic that i can reuse everywhere.
It's depend, too, on what you put i your files, but if it's some helper like a UserManager.php on which you create some new request (And you declare it as a service), the Manager folder it's a good place to start.
You can find this folder Manager in many bundle like fosElasticaBundle
ps: sorry for my english, i hope it's clear enough:)

Create a bundle in vendors in Symfony2

I am trying to create a bundle in vendors in Symfony2.
MyBundle(MineUserBundle) needs to inherit other two bundle in vendors FOSUserBundle and HWIOAuthBundle.
How to get inherit both bundles properties, templates etc in Mybundle?
vendors
FOSUserBundle
HWIOAuthBundle
MineUserBundle
Here MineUserBundle needs to have all features of other two bundle.
How to get the process work?
Since you are using namespaces it does not mater into which folder you are going to deploy your Bundle. Just include use statements in your bundle with ones you need:
use Path\To\FOS\UserBundle;
use Path\ToHWIO\AuthBundle;
And you can extend whatever you need.
But if you really want to install your bundle to vendor directory, then you have to configure define it in your deps file.
[YourVendorYouBundle]
git=git#github.com:yourRepository/YourVendorYouBundle.git
target=/bundles/YourVendor/YouBundle

Multi-Bundles in Symfony 2

I create project from Symfony 2, but I have a problem:
In project have multi-bundles(ex: AdminBundle and FontEndBundle)
Case 1: Doctrine orm and Entities generate on FrontEndBundle, then from AdminBundle, I will call Entity via FrontEndBundle:Object it work OK.
Case 2: I want to config structure folow
src/Project/
Model/Entity
OrmYml/doctrine/orm
Bundles(contains FontEndBundle & AdminBundle)
Extensions
In case 2, How do I config Entity mapping to generate Entities to src/Model/Entity directory ? Because when I using Command: doctrine:generate:entities Project/Model/Entity,
error : Namespace "Project\Model\Entity" does not contain any mapped
entities.
How do you declare your entities ?You should put your entities within a bundle. You cant have them outside a bundle .
Usually, Symfony developpers create a third bundle called "CoreBundle" (for example) where you place all the shared resource between your three bundles, namely the entities, some services (like twig extensions), config (with service.xml/yml), ... Also, you can delete the controller and views directories in this bundles, which are useless (don't forget to clean the app/config/routing.yml file by removing the CoreBundle controller injection) !
Then just call your entities in the right bundle with:
use MyName\Bundle\CoreBundle\Entity\MyEntity;
Never create a model repertory not in a bundle, this is not the Symfony philosophy and you are really wrong !

Resources