I use symfony's ProjectConfiguration.class.php for configuring doctrine's connection:
public function configureDoctrineConnectionCertby(Doctrine_Connection $conn)
{
$conn->setListener(new MyListner());
}
Where should I define MyListner class within a symfony project?
You should make a lib/Doctrine (or lib/doctrine/Doctrine - this is what Symfony does inside of sfDoctrineGuardPlugin) folder for all Doctrine files. Since Listeners are Doctrine files not Symfony files, you should be following Doctrine naming conventions for them. In this case, MyListener would go in:
/lib/Doctrine/Record/Listener/MyListener.php
This is the "proper" way to do this. It would of course work if you simply threw the files in /lib or /lib/Doctrine.
If you want your listener to be available at the project level, put it in the /project/lib/ folder. If you just want it to be available at the module level, put it in the /project/apps/module/lib/ folder.
Related
I'm trying to move my spec folder into a zend 2 module folder. I don't totally understand how phpspec determines where the source folder is. How can I configure it to use the below folder structure.
\ModuleName
\spec
\ModuleName
\Class.php
\src
\ModuleName
ClassSpec.php
...
...
You can configure spec and src paths, see the docs:
http://phpspec.net/en/latest/cookbook/configuration.html
If that doesn't solve your issue, you'll need to write a phpspec extension and provide a custom resource locator. Here's a similar example for Symfony:
https://github.com/phpspec/Symfony2Extension/blob/master/src/PhpSpec/Symfony2Extension/Locator/PSR0Locator.php
In my standard Symfony2-app I'm having a bunch of bundles with some entities. Some of these entities are not located in the standard folder the automapping of doctrine finds out (e.g. /src/Acme/DemoBundle/Entities) but in a different location.
I could easily use config.yml to tell doctrine to use a different location like this:
doctrine:
orm:
auto_mapping: false
mappings:
AcmeDemoBundle:
type: annotation
prefix: Acme\DemoBundle\Entities\
dir: %kernel.cache_dir%\Acme\DemoBundle\Entities
This works. But say I'm having 10 bundles with a different mapping the config.yml gets bloated very fast. Is there another way, e.g. with a CompilerPass or via DependencyInjection, so I don't need to add all entities in my config.yml? I already looked into the DoctrineBundle, but had no luck so far.
To answer myself:
the most simple way is to adjust the autoloading, there is no need to modify the settings. In Symfony's standard distribution in autoload.php you have to add another location to the registerNamespace-method:
$loader->registerNamespaces(array(
[...]
'Foo' => array(__DIR__.'/../src/dirA', __DIR__.'/../src/dirB')
));
Doctrine will then look for entities in the "Foo" namespace first in dirA and then in dirB if not found.
You can include other configuration files using imports
# yaml
imports:
- { resource: entities.yml }
<!-- xml -->
<imports>
<import resource="enditites.xml" />
</imports>
// PHP
$loader->import('entities.php');
You don't even have to stick to a single file type. It's possible to import an xml configuration file to a yaml file, for example.
so, I have some php files in my apps/myprogram/lib folder. e.g. apps/myprogram/lib/myLibA.class.php
When I run in my modules/actions/ scripts, and try to use the functions in myLibA, I cannot. because symfony complains that the myLibA class is not defined.
do I need to specify anywhere in the symfony framework that myLibA.class.php is a required library?
Symfony's autoloader looks by default for your classes in the top-level <project>/lib directory. Any file in that directory or below (with the exception of "vendor") will be searched for classes. Symfony searches for any .php file with class declarations and adds them to the autoload system.
Additionally, you can add search paths in your application's autoload.yml file. For example, for one of my applications I've put a third-party Flickr library in <project>/vendor/phpFlickr, and my <project>/apps/frontend/config/autoload.yml file looks like:
autoload:
vendor_php_flickr:
path: %SF_LIB_DIR%/vendor/phpFlickr
recursive: on
That allows all classes below .../vendor/phpFlickr to be autoloaded.
this is an alternative way, you can define/add to your preExecute this:
public function preExecute()
{
$this->getContext()->getConfiguration()->loadHelpers('Foo', 'Bar');
}
taken from (http://oldforum.symfony-project.org/index.php/m/92916/)
What is the best way to put custom library or helper methods in symfony?
I am using doctrine with my project. One place I consider to put is under project_root/lib/vendor/MyClasses/
But if I want to create a class or helper function which will use some core symfony/doctrine methods and return a result then how to do that and where should I put it?
I want it to call from different modules to avoid code duplication.
As for the custom library part of the question, you might probably want to put your external library into the lib/vendor folder. Since symfony doesn't automatically load everything that's in there, you'll have to tell its autoloader to do so.
You can either extend your config/ProjectConfiguration.class.php (as described here) or (and this is the much simpler and cleaner way) you add them to your config/autoload.yml (you might have to create this one).
For the latter option, this is a great place to start looking at.
It seems to be a duplicate question.
As asked in symfony's 1.2 "Definitive Guide" docs :
Helper functions (regular PHP functions returning HTML code) should be saved in a file called FooBarHelper.php, where FooBar is the name of the helper group. Store the file in the apps/myapp/lib/helper/ directory (or in any helper/ directory created under one of the lib/ folders of your project) so it can be found automatically by the use_helper('FooBar') helper for inclusion.
So, if you want to create custom helper FooBar for foo() function, create file lib/helper/FooBarHelper.php :
function foo() {echo "foo!"; }
to use it in your template:
use_helper('FooBar')
....
foo(); //outs "foo!"
I'm developing a Grails App. I have about 20 Controllers right now and there will be more. Is there a way to Group the Controllers in functional Packages? I would like to have something like:
grails-app/administration/<controller classes>
grails-app/usercontent/<controller classes>
grails-app/publiccontent/<controller classes>
The best would be if the Package would not appear in the URL.
You can do something similar by putting your controllers into Java/Groovy packages:
package administration
class UserController { ... }
and placing the source code into corresponding sub-directories of grails-app/controllers/, eg. grails-app/controllers/administration/UserController.groovy. This won't change the default URL Mapping (ie. the package name is not included in the URL). Note however, that your controller names have to be unique even across different packages!
I'm not aware of any easy approach to achieve the directory layout you suggested (no controller/ in the path).