This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Fabien Potencier c8df0ccf79 merged branch lmcd/master (PR #1290)
Commits
-------

49dd558 Couple more CS fixes
5a986bf Add $keysCount & minor CS fix
91f4097 [Routing] Better nesting for RouteCollections in dumped URL matcher classes With this change, a route prefixed with '/blogger' will be nested inside '/blog' (for example)

Discussion
----------

[Routing] Better nesting for RouteCollections in dumped URL matcher classes

Consider the following routing file:

    _blog:
        resource: "@AcmeDemoBundle/Resources/config/routing/BlogController.yml"
        prefix:   /blog

    _blogger:
        resource: "@AcmeDemoBundle/Resources/config/routing/BloggerController.yml"
        prefix:   /blogger

    _bloggeroo:
        resource: "@AcmeDemoBundle/Resources/config/routing/BloggerooController.yml"
        prefix:   /bloggeroo

    _blogtest:
        pattern:   /blogtest
        defaults:  { _controller: AcmeDemoBundle:Blog:test }

    login:
        pattern:   /login
        defaults:  { _controller: AcmeDemoBundle:Security:login }

Note: Each imported file contains two simple blog/blogger/bloggeroo routes (e.g. blog_index & blog_view)
With this change, the cached URL matcher looks something like this:

```php
<?php
class appprodUrlMatcher
{
  public function match($pathinfo)
  {
      $allow = array();

      if (0 === strpos($pathinfo, '/blog')) {
          // blog_index
          if (preg_match('#^/blog/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

          // blog_view
          if (preg_match('#^/blog/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

          if (0 === strpos($pathinfo, '/blogger')) {
              // blogger_index
              if (preg_match('#^/blogger/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

              // blogger_view
              if (preg_match('#^/blogger/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

              if (0 === strpos($pathinfo, '/bloggeroo')) {
                  // bloggeroo_index
                  if (preg_match('#^/bloggeroo/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

                  // bloggeroo_view
                  if (preg_match('#^/bloggeroo/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {...}

                  throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
              }

              throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
          }

          // _blogtest
          if ($pathinfo === '/blogtest') {...}

          throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
      }

      // login
      if ($pathinfo === '/login') {...}

      throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
  }
}
```

As far as I can see, you can throw the 404 earlier (as I've done), as by nesting all these possibilities, there is no chance another route will be matched outside the parent if statement.

---------------------------------------------------------------------------

by stloyd at 2011/06/11 08:37:15 -0700

You should follow Symfony [CS rules] (http://symfony.com/doc/current/contributing/code/standards.html), also you should modify tests.

---------------------------------------------------------------------------

by lmcd at 2011/06/11 08:46:32 -0700

Besides the inline `continue` statement, I can't see spot any other CS violations. I'm on to the updated tests.

---------------------------------------------------------------------------

by fabpot at 2011/06/13 02:19:14 -0700

What if you have this:

    _blog:
        resource: "@AcmeDemoBundle/Resources/config/routing/BlogController.yml"
        prefix:   /blog

    login:
        pattern:   /login
        defaults:  { _controller: AcmeDemoBundle:Security:login }

    _blogger:
        resource: "@AcmeDemoBundle/Resources/config/routing/BloggerController.yml"
        prefix:   /blogger

You cannot send 404 early because the same `blog` prefix is used in two different places. I can see many things that will break with this patch. I'm pretty sure we can enhance the performance of the existing code but let's do that after 2.0.

---------------------------------------------------------------------------

by lmcd at 2011/06/13 05:04:03 -0700

@fabpot: The code was designed so it would work in these kind of circumstances. I ran the dumper against the routing file you provided and this is the output below.

I see no reason why you can't throw the 404 earlier, as by nesting everything with similar prefixes, we're eliminating the possibility that a route will ever be matched outside of that if statement.

```php
public function match($pathinfo)
{
    $allow = array();

    if (0 === strpos($pathinfo, '/blog')) {
        // blog_index
        if (preg_match('#^/blog/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {
            ...
        }

        // blog_view
        if (preg_match('#^/blog/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {
            ...
        }

        if (0 === strpos($pathinfo, '/blogger')) {
            // blog_index
            if (preg_match('#^/blogger/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {
                ...
            }

            // blog_view
            if (preg_match('#^/blogger/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) {
                ...
            }

            throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
        }

        throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
    }

    // login
    if ($pathinfo === '/login') {
        ...
    }

    throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}
```

---------------------------------------------------------------------------

by fabpot at 2011/06/13 07:48:49 -0700

Can you add some more tests to ensure that everything works fine and that we won't have regressions later on? Thanks.
2011-06-15 09:09:35 +02:00
src/Symfony merged branch lmcd/master (PR #1290) 2011-06-15 09:09:35 +02:00
tests merged branch vicb/file (PR #1317) 2011-06-15 08:57:16 +02:00
.gitignore Added vendor directory to .gitignore 2010-06-24 10:44:28 +02:00
autoload.php.dist fixed autoloader when Swiftmailer is not installed 2011-06-14 21:57:58 +02:00
check_cs fixed root search path to include only './src' and './tests' 2011-06-08 18:11:05 +02:00
LICENSE added the LICENSE file for the YAML component 2011-02-18 11:52:11 +01:00
phpunit.xml.dist [Security] cleaned up opt-in to benchmark test 2011-03-06 20:06:13 +01:00
README.md updated the README file 2011-05-24 17:27:37 +02:00
UPDATE.ja.md update UPDATE.ja.md 2011-06-10 01:25:45 +09:00
UPDATE.md merged branch vicb/file (PR #1317) 2011-06-15 08:57:16 +02:00
vendors.php Adding more escapes to shell params 2011-06-10 11:57:54 -04:00

README

What is Symfony2?

Symfony2 is a PHP 5.3 full-stack web framework. It is written with speed and flexibility in mind. It allows developers to build better and easy to maintain websites with PHP.

Symfony can be used to develop all kind of websites, from your personal blog to high traffic ones like Dailymotion or Yahoo! Answers.

Requirements

Symfony2 is only supported on PHP 5.3.2 and up.

Installation

The best way to install Symfony2 is to download the Symfony Standard Edition available at http://symfony.com/download.

Documentation

The "Quick Tour" tutorial gives you a first feeling of the framework. If, like us, you think that Symfony2 can help speed up your development and take the quality of your work to the next level, read the official Symfony2 documentation.