(其實packagist.org更好用:))php
小哥 若是你有天心血來潮要寫本身的bundle 請先上knp跟packagist看看是否是已經有了相似的bundle 有的話 請fork它 幫助它改善的更穩定更強大 這樣能節約你的時間css
AcmeMyBundle.php Command MyCommand.php Controller MyController.php Doctrine ORM Foo.php FooRepository.php Model Foo.php Resources config doc meta public views ...
參考CMF : Bundle Standards git
DependencyInjection Compiler MyCompilerPass.php Configuration.php AcmeMyExtension.php
use Symfony\C..\D..I..\ContainerBuilder as CB; use Symfony\C..\Config\FileLocator; use Symfony\C..\HttpKernel\D..I..\Extension; use Symfony\..\D..I..\Loader\XmlFileLoader; class AcmeMyExtension extends Extension { public function load(array $configs, CB $dic) { $configuration = new Configuration(); $config = $this->processConfiguration( $configuration, $configs ); $dir = __DIR__ . '/../Resources/config'; $locator = new FileLocator($dir); $loader = new XmlFileLoader($dic, $locator); $loader->load('services.xml'); foreach ($config as $key => $val) { $dic->setParameter('acme_my.'.$key, $val); } } }
use Symfony\C..\Config\Definition as Def; class Configuration implements Def\ConfigurationInterface { public function getConfigTreeBuilder() { $treeBuilder = new Def\Builder\TreeBuilder(); $treeBuilder->root('acme_my') ->children() ->scalarNode('host') ->isRequired()->cannotBeEmpty()->end() ->scalarNode('port') ->defaultValue(2000)->end() ->scalarNode('timeout') ->defaultValue(30000)->end() ->end() ; return $treeBuilder; } }
{ "name": "acme/my-bundle", "type": "symfony-bundle", "description": "This Bundle is an example", "license": "MIT", "authors": [ { "name": "Duffy Duck", "email": "diffy@acme.com" } ], "require": { "php": ">=5.3.3", "symfony/framework-bundle": "~2.2", }, "autoload": { "psr-0": { "Acme\\MyBundle": "" } }, "target-dir": "Acme/MyBundle" }
Controller
MyControllerTest.php
Functional
HomepageTest.php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class HomepageTest extends WebTestCase { public function testContents() { $client = $this->createClient(); $crawler = $client->request('GET', '/'); $s = $client->getResponse()->getStatusCode(); $this->assertEquals(200, $s); $crawler->filter('h1:contains(Homepage)'); $this->assertCount(1, $count); $crawler->filter('ul.menu_main li') $this->assertCount(13, $count); } }
language: php php: - 5.3 - 5.4 - 5.5 env: - SYMFONY_VERSION=2.2.* - SYMFONY_VERSION=2.3.* - SYMFONY_VERSION=dev-master before_script: - composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source - vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh script: phpunit --coverage-text