Documentation symfony

Hi,

I want to start using the symfony2 framework for my coding. Normally, when I want to learn a new programming language the first start is buying a good book. After I have learned the basics from the book I start with a small project. During the coding of my first projects I rely heavily on the syntax documentation.

The symfony website has The Book, The Cookbook, and The Components, so the book part is covered. With regard to the syntax documentation I run into a problem. Symfony has some form of syntax documentation at api.symfony.com. However, I doesn’t come close to PHP documentation on php.net. The php manual for example contains the a syntax documentation and a working example.

Is there a good source for syntax documentation for symfony2? Or are there better documented frameworks around that I could give a try.

Any help would be much appreciated!

php.net is a rare example of how to document a language. I wish more projects had something similar. But the examples tend to be function specific. Not a whole lot of application oriented stuff.

On the Symfony 2 documentation home page, right under Book, Cookbook, Components there is a section called Reference which documents most of the configuration options for the framework. Between these four sections I really think most stuff is covered including plenty of working examples. Especially if you take the time to work through the book.

There is an extremely active Symfony 2 tag on stack overflow. In most cases you can simply copy/paste in a question or error message and your browser search will take you right to the stack overflow answer.

Under News on the Symfony 2 home pages there are weekly “Week in Symfony” blog posts. At the end of each one there are links to various sites discussing Symfony. Of course there are mailing lists, dedicated forums and chat rooms. Books are also starting to come out.

Are there better documented and supported frameworks? Perhaps. But go ahead and try a small project. I really think the community will have you covered.

Hi, thank you for you reply. So you are basically saying that you need to learn symfony by trial and error. Try every function and then see what it does.
Or would it be better to start with Symfony version 1 and then work up to the most current version. B.T.W. some of the external bundles seem to better documented.

It all goes a bit against the benefit of using a framework. In this case you do not have to re-invent the wheel but discover how it works. :wink:

Yes, sometimes the learning curve isn’t worth the time and effort. Especially if it’s a one-off use.
But it’s always good to know more stuffs if you think you may use it again.

I personally love using the Laravel framework, in the past I’ve used Symfony but found it either too chunky for my needs or hard to develop with due to the lack of examples. Since moving to Laravel it’s like a walk in the park, it’s very well documented and is tiny compared to Symfony which I like and leaves a very tiny memory footprint if you require something with performance in mind.

All-in-all I haven’t found anything that I dislike about the framework especially because I’m primarily a front end developer, it’s just awesome.

Or would it be better to start with Symfony version 1 and then work up to the most current version. B.T.W. some of the external bundles seem to better documented.

Nope. Symfony 1 and 2 are completely different frameworks. No point in looking at Symfony 1 at all.

Not at all. You look at the book. http://symfony.com/doc/current/book/index.html. Starts with a couple of chapters on basic web design then talks about installing it. Then it shows you how to setup a page and then dives into details on individual components.

I’m honestly confused by what you are asking for. Can you link to a framework that has what you consider to be proper documentation? Because all the ones that I have seen follow more or less the same pattern.

What I have done so far is:

  • Read the the book, the cookbook and some of the components a couple of times;
  • Installed Netbeans and Eclipse with support for symfony and doctrine;
  • Installed composer, git, etc;
  • Tried some tutorial like symblog and jobeet. I solved all the problems that came up;
  • I understand the workings of the MVC;
  • I have created twig templates and I really like them;
  • Created the routes in for the controllers.

Next step is to start coding the controller. Now I ran into the same problem I already had with the documentation on the symfony site. Where can I find an explanation of how a function works. I can start coding regular object oriented php code in the controllers, but that would be beside the point. On the php.net manual site you have an explanation of how a certain function works. The same goes for MySQL, when you need to find out the options for a command you look at the online manual.

Where can I find an explanation of how a function works.

Let’s try to narrow this down. Can you give an example of a specific function that you need to know the workings of?

Frameworks don’t try to take over all your code. Most of the application stuff inside of a controller really should just be regular object oriented code. But again, please describe a specific function that you are having trouble with or need more documentation on and maybe we can help out.

An example would be to fetch a specific product from a database and display it on a product page. The products are inside a database with the following structure: id, name, description, etc. I don’t want to use the Doctrine or Propel because data abstraction is not needed. Furthermore, I am already familiar with MySQL / SQL and I want to focus my effort on learning Symfony.

I want use user friendly URLs for bookmarking and search engine optimization. I therefore use the GET method to retrieve the specific product page. In order to avoid SQL-injection or other forms of hacking I would need to sanitize and validate the user input. The router gives the ability to check if the user input is a integer by using " \d+".

product_show:
  path:     /product/{id}
  defaults: {_controller: TestBundle:Product:show }
  requirements:
      id:     \d+

With the \d+ the router the slug - id - is checked if it contains digits. This is in the book
http://symfony.com/doc/current/book/routing.html
. Next step for me is to see if the router can do more things. Let’s say I want it to display the product list page if a wrong input is given. Or that I want to include a search engine on my site. Or that I want to use text instead of numbers. Then I need to know more about the options of the router. Where can I find this info?

To retrieve a specific product from the product database I use something like the following code:


class ProductController {

public function showAction() {

$sql = "SELECT id, name, description FROM products WHERE id = ...";

$stmt = $this -> getEntityManager() -> getConnection -> prepare($sql);

$stmt -> execute();

$results = $stmt -> fetchAll();

 // some code

 return $this->render('TestBundle:Product:show.html.twig', array('id' => $id, 'name' => $name));
}

} 

Where can I find more information for the functions like getEntityManager(). In this case it is simpler because I can use the doctrine pages. But let say this question is about forms and validators.

You’ll probably have to do that in a controller, because you’ll need to try to fetch the ID before you can know if it’s a real record or not.

if (empty($results)) {
    $response = $this->forward('TestBundle:Product:list');
    
    return $response;

    // Though, I actually think returning a 404 would be better
    // than forwarding or redirecting to the product list
}

The HTML for this would be entirely in your templates, and probably in the layout template. And then you would have a /search route and an associated controller.

Between the book, the cookbook, the component, and the API, what is it that you feel like you’re missing?

Forms and validators are both covered in the book, the cookbook, the components, and the API. This thread is awfully weird because you started it out showing that you know where to find the documentation, and then every question thereafter has been to ask where’s the documentation.

Avoiding SQL injection is absolutely a good thing, but this is probably not the right place to be worrying about it. I noticed in your controller that you’re using prepared statements. That’s good. If you’re preparing and binding – and not concatenating values to the SQL string – then you’re already protected against SQL injection, no matter what value the ID holds.

This thread is awfully weird because you started it out showing that you know where to find the documentation, and then every question thereafter has been to ask where’s the documentation.

@Jeff_Mott the difference is that you and others consider the introduction text in the The Book, The Cookbook and The Components as the documentation. For me the documentation only provides examples what Symfony2 can do. The documentation is by no means complete. And normally that would not be a problem because I could then look at the syntax documentation.

For example, from the book I know the router can filter the input and one of the options is to check for digits (\d+). Where can I find the filter for letters? Let’s say you would like to know what the filter options
are where do you look?

Another example would be the function forward in your code. Let’s say I would like to find the difference between forward () and response() or render(). Can I include an array of database results in the forward function? Where can I find this?

The last example would be if you want to connect to a database but I am unable to connect to the database. I want Symfony to throw an error message that informs the user (‘Unable to connect to database’). I know that symfony has specialized error messages, but where can I find all the exception messages and their meaning.

B.T.W. how do you typically learn Symfony. Prior experience with other frameworks? Professional working experience? Reading code from colleagues? A book? Training?

Jeff did a great job. Let me just focus on your database connection.

You found: $this → getEntityManager() → getConnection() which returns a Doctrine 2 database connection object which is really just a thin wrapper over a PDO object.

All documented here: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/

Let’s assume that you really don’t want any Doctrine at all and just want to use straight PDO. This is where you start to get into services and dependency inject stuff. You would start by configuring a service called pdo.

# services.yml
services:
pdo:
    class: PDO
    arguments:
        dsn:      '%database_dsn%'      # mysql:dbname=appgames
        user:     '%database_user%'
        password: '%database_password%'
    calls:
        - [setAttribute, [ 3, 2]] # \PDO::ATTR_ERRMODE,           \PDO::ERRMODE_EXCEPTION
        - [setAttribute, [19, 2]] # \PDO::ATTR_DEFAULT_FETCH_MODE \PDO::FETCH_ASSOC

You can read here how to load the services file using your Dependency Injection extension: http://symfony.com/doc/current/cookbook/bundles/extension.html

Getting your first service to work will take a bit of effort but once you have the process down then it becomes quite easy. Knowing how and when to use services is a key part of Symfony 2. In fact, most frameworks nowadays use a dependency injection container of some sort.

Once your service is configured you would use it in your controller like this:

// Notice that product id will be passed in as a parameter
public function showAction($route,$id)
{
    // Grab the pdo object from the service container
    $pdo = $this->get('pdo');

    // Just do a pdo query per the php manual.
    // Using prepared statements will protect against sql injection.
    $sql = "SELECT id, name, description FROM products WHERE id = :id";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array('id' => $id));
    $rows = $stmt->fetchAll();

    $product = count($rows) ? $rows[0] : null;
    print_r($product);

You also asked about forms. One nice thing about Symfony 2 forms is that they can operate on either objects (Doctrine entities) or plain arrays. So you can follow the examples in the documentation and just pass your product array as the input.

I went looking for detailed route configuration options and I didn’t find a nice summary section. Maybe Jeff knows.

However, a careful reading of the existing documentation will answer many of your questions. For example:

The \d+ requirement is a regular expression that says that the value of the {page} parameter must be a digit (i.e. a number).

So we can see the Symfony uses regular expressions for pattern matching. If you want to match say characters then you just plugin the appropriate expression. There are tons of resources on regular expressions. No real need for Symfony to go into more details.

Ditto what ahundiak said. Both the book and the API documentation mention that route requirements are regular expressions. If you don’t know what regular expressions are, then that would certainly explain the confusion. You can read about them in the PHP manual for PCRE Patterns. So if you wanted a particular route param to match only letters, that would be [a-z]+

The Book and the API documentation.

The Cookbook: How to Customize Error Pages

Thank you for your comments. I am well aware of regular expressions. My point is that the book only mentions that you could use filters (e.g. digits). I want to find out if there where more options. Coming from PHP I would have guested to use

id: [0-9]+
to check for numbers and
id: [a-z]+
for letters.

With regard to the documentation of functions like forward(), response() and render() I think the syntax documentation of for example PHP (php.net) , MySQL (http://dev.mysql.com/doc/refman/5.7/en/) and HTML / CSS (www.w3.org) is easier to understand.

B.T.W. I didn’t mean to criticize @Jeff_Mott. The quote option didn’t work the way I expected.