Symfony2 for Midgard Developers
- 11. Under MIT License
Copyright (c) 2004-2011 Fabien Potencier
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
- 14. Symfony2 is a collection of standalone PHP components
bundled together into a single framework
- 16. ✔ Authentication and Authorization
✔ Forms and Validation
✔ Templating
✔ Logging
✔ Asset Management
✔ Routing
✔ Internationalization
✔ Console Tasks
✔ Caching
- 20. Midgard MVC & MidCOM
<?php
class example_mycomponent_controllers_some
{
Symfony2
<?php
namespace ExampleMyBundleController;
use SymfonyFrameworkWebBundleController;
class SomeController extends Controller
{
- 23. <?php
$crRoot = realpath(__DIR__ . '/..');
// register the autoloaders
require "{$crRoot}/SplClassLoader.php";
// Tell where Midgard stuff is
$midgardAutoloader = new SplClassLoader('Midgard', "{$crRoot}/src");
$midgardAutoloader->register();
// Tell where code from some other project is
$phpcrAutoloader = new SplClassLoader('PHPCR', "{$crRoot}/lib/PHPCR/src");
$phpcrAutoloader->register();
- 29. $ php app/check.php
********************************
* *
* Symfony requirements check *
* *
********************************
php.ini used by PHP: /etc/php5/cli/php.ini
** Mandatory requirements **
OK Checking that PHP version is at least 5.3.2 (5.3.5-1ubuntu7.2 installed)
OK Checking that the "date.timezone" setting is set
OK Checking that app/cache/ directory is writable
...
- 30. Dependencies are defined in the deps file
[AsseticBundle]
git=http://github.com/symfony/AsseticBundle.git
target=/bundles/Symfony/Bundle/AsseticBundle
version=v1.0.0RC2
- 31. Now you could just use the web dir
stuff in your regular web server.
- 32. However, we'll be using AppServer-in-PHP
$ pear channel-discover pear.indeyets.pp.ru
$ pear install indeyets/aip
- 33. Add AiP integration as dependency
[AppServerBundle]
git=http://github.com/bergie/MidgardAppServerBundle.git
target=Midgard/AppServerBundle
Install with $ php bin/vendors install --reinstall
- 35. Add Midgard namespace to autoloader
$loader->registerNamespaces(array(
...
'Midgard' => __DIR__.'/../vendor',
- 37. Now, to write some code!
$ php app/console generate:bundle
Let Symfony2 create a Acme/ExampleBundle for you.
- 42. How did this template get loaded?
/**
* @Template()
*/
...
return array('name' => $name);
⇒ Defaults to Bundle/Resources/views/Controller/action.html.twig
We could also do:
return $this->render(
'AcmeExampleBundle:Default:index.html.twig',
array('name' => $name)
);
- 43. Page generation process:
1. Match the URL to a route (/hello/{name})
/hello/World ⇒ /hello/{name}
2. Instantiate Controller
new AcmeExampleBundleControllerDefaultController
3. Run action method with arguments
->indexAction('World')
3. Controller returns a Response
Hello World!
- 48. In Midgard you only define the elements you want to override
In Twig you define a new root element that inherits and then
overrides.
- 51. You can also use other templating engines
TAL, PHP, MidCOM, ...
http://symfony.com/doc/2.0/cookbook/templating/PHP.html
- 54. Registering routes with PHP
app/config/routing.yml
AcmeExampleBundle:
resource: "@AcmeExampleBundle/Controller/"
type: annotation
prefix: /
In Controller action methods:
/**
* @Route("/hello/{name}")
*/
- 55. Registering routes with YAML
AcmeExampleBundle:
resource: "@AcmeExampleBundle/Resources/config/routing.yml"
prefix: /example
src/Acme/ExampleBundle/Resources/config/routing.yml
hello_user:
pattern: /hello/{name}
defaults: { _controller: AcmeExampleBundle:Default:index}
(format is NamespaceBundle:Controller:action)
- 56. $ php app/console router:debug
[router] Current routes
Name Method Pattern
_welcome ANY /
_demo_login ANY /demo/secured/login
_security_check ANY /demo/secured/login_check
_demo_logout ANY /demo/secured/logout
...
_configurator_final ANY /_configurator/final
acme_example_default_index ANY /example/hello/{name}
- 57. Supporting other output formats
/**
* @Route("/hello/{name}.{_format}", defaults={"_format"="html"})
*/
http://localhost:8001/hello/World and
http://localhost:8001/hello/World.html
will both work
- 62. Add Midgard ConnectionBundle as dependency
[MidgardConnectionBundle]
git=git://github.com/bergie/MidgardConnectionBundle.git
target=Midgard/ConnectionBundle
Install with $ php bin/vendors install
Enable in app/AppKernel.php
$bundles = array(
...
new MidgardConnectionBundleMidgardConnectionBundle()
);
- 64. Now you can create a database
$ php app/console midgard:connection:init
- 65. Using Midgard in controllers
src/Acme/ExampleBundle/Controller/DefaultController.php
/**
* @Route("/hello/{name}.{_format}", defaults={"_format"="html"})
* @Template()
*/
public function indexAction($name)
{
$qb = new midgard_query_builder('midgard_person');
$qb->add_constraint('firstname', '=', 'Midgard');
$persons = $qb->execute();
return array('name' => $persons[0]->firstname);
}
- 70. Add Midgard MidcomCompatBundle as dependency
[MidcomCompatBundle]
git=git://github.com/bergie/MidgardMidcomCompatBundle.git
target=Midgard/MidcomCompatBundle
Install with $ php bin/vendors install
Enable in app/AppKernel.php
$bundles = array(
...
new MidgardMidcomCompatBundleMidgardMidcomCompatBundle()
);
- 71. Install Flack's version of MidCOM
$ git clone https://github.com/flack/openpsa.git
Copy schemas from openpsa/schemas to MgdSchema dir
app/config/config.yml
midgard_midcom_compat:
root: "%kernel.root_dir%/../openpsa/lib"
framework:
templating: { engines: ['twig', 'midcom'] }
Run $ php app/console midgard:connection:init
- 72. Enable component in app/AppKernel.php
$bundles = array(
...
new MidgardMidcomCompatBundleBundleComponentBundle(
'net.nehmer.blog')
);
Add a component to routing configuration:
NetNehmerBlog:
resource: "net.nehmer.blog"
type: midcom
prefix: /blog
- 74. Loading a layout for your MidCOM views
app/config/config.yml
midgard_midcom_compat:
layout: "MidgardMidcomCompatBundle::layout.html.twig"
- 75. Some areas to follow:
• Symfony CMF: node-based routing, etc
• PHPCR: standard content repository APIs
• MidCOM and Midgard MVC compat work