15

Any information on how to use symfony's decoupled components?

I'm rereading the docs but there's nothing on the topic besides a general message of "They are very very decoupled" and 1 tutorial that makes use of Request and Response.

There's also one badly ranked answer of Using symfony2 routing component (outside of symfony2)

Also having a look at a tutorial for the standalone Form component doesn't actually excite me how pleasant this is.

I need the routing, yaml, and session.

Community
  • 1
  • 1
antitoxic
  • 3,746
  • 1
  • 37
  • 46

5 Answers5

7

The first component you should use is ClassLoader. You can also use spl_autoload_register, but you're using Symfony, so why shouldn't you use its own autoloading library? Add the following at the top of the script:

use Symfony\Component\ClassLoader\UniversalClassLoader;

require_once '/path/to/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';

$loader = new UniversalClassLoader();
$loader->register();

$loader->registerNamespaces(array(
    'Symfony' => '/path/to/symfony/src',
));

Using the Yaml component is really easy:

use Symfony\Component\Yaml\Parser;
$data = Parser::parse('yaml string');

For the other components, you'll have to read the API documentation, as there are no tutorials yet.

Alessandro Desantis
  • 14,098
  • 1
  • 26
  • 32
  • Actually a link to the API docs is not very helpful. The problem as I see is all components make use of at least 2 other components. So they are pretty much tied to them. The YAML component on its own is easy to decouple since sf1. I need YAML usage for routing configuration, not just YAML. – antitoxic Oct 24 '11 at 15:16
  • Looks like old version of Symfony is used in this example. It doesn't work and gives PHP error. – Fedir RYKHTIK Jan 14 '16 at 11:34
7

Interestingly, Fabien Potencier just published a blog post which contains snippets of how to use the most common components. See the second half of this post for details.

richsage
  • 26,912
  • 8
  • 58
  • 65
  • I've just [added a comment](http://fabien.potencier.org/article/49/what-is-symfony2#comments). I hope we can get some more standalone examples. – antitoxic Oct 25 '11 at 10:35
5

I've written a tutorial which might help you, on using decoupled Symfony components in your project.

It shows how to use the console component as an example, but the logic is the same for other components.

Tal Ater
  • 1,121
  • 1
  • 10
  • 17
3

Composer is the answer.

This video here http://www.youtube.com/watch?v=QOldVDVYnAE has a simple and straight forward step by step that answers your question.

EDIT on august 26th 2020: The video creator has made it private. Sorry, nothing I can do about it.

Francisco Luz
  • 2,775
  • 2
  • 25
  • 35
0

Symfony2 example

1) Install the component You need with composer in new folder

composer require symfony/yaml

2) Create the script yaml.php

<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
$value = $yaml->parse('invoice: 3484');
var_dump($value);

3) Run the script

php yaml.php
Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68