Apache route dumper #521

Closed
weaverryan opened this Issue Jul 7, 2011 · 6 comments

Comments

Projects
None yet
5 participants
@weaverryan
Member

weaverryan commented Jul 7, 2011

Add documentation - to the "performance" chapter, or possibly a cookbook article referenced by that chapter - about how to dump routes to Apache.

@GromNaN

This comment has been minimized.

Show comment Hide comment
@GromNaN

GromNaN Jul 31, 2011

Contributor

+1 for this doc. It should be linked with "How to setup a virtual host".

@weaverryan Is it for the cookbook or the book ?

Contributor

GromNaN commented Jul 31, 2011

+1 for this doc. It should be linked with "How to setup a virtual host".

@weaverryan Is it for the cookbook or the book ?

@jalliot

This comment has been minimized.

Show comment Hide comment
@jalliot

jalliot Sep 9, 2011

Contributor

Something related: adding a note about ApacheRequest instead of Request which simplifies some processing in the case we are using Apache.
To use it, simply change Request::createFromGlobals() in the front controller by ApacheRequest::createFromGlobals() (and of course also the use statement).

It might be appropriate to add ApacheRequest to the list of compiled classes if we're using it BTW... (only Request is from what I see inside the FrameworkBundle extension)

Contributor

jalliot commented Sep 9, 2011

Something related: adding a note about ApacheRequest instead of Request which simplifies some processing in the case we are using Apache.
To use it, simply change Request::createFromGlobals() in the front controller by ApacheRequest::createFromGlobals() (and of course also the use statement).

It might be appropriate to add ApacheRequest to the list of compiled classes if we're using it BTW... (only Request is from what I see inside the FrameworkBundle extension)

@jalliot

This comment has been minimized.

Show comment Hide comment
@jalliot

jalliot Apr 22, 2012

Contributor

Ok so I've tested how to use the ApacheMatcher today so here it goes:
Add this to app/config/config_prod.yml:

parameters:
    router.options.matcher.cache_class: ~
    router.options.matcher_class: Symfony\Component\Routing\Matcher\ApacheUrlMatcher

The first option disables the generated router cache class which is of no use when using ApacheUrlMatcher and would bypass it...
The second one changes the default UrlMatcher to the Apache one.
A good thing to note is that ApacheUrlMatcher extends UrlMatcher so that even if you don't regenerate the url_rewrite rules, it will work (because at the end of ApacheUrlMatcher::match a call to parent::match is done). But of course, when doing so we lose the advantage of this matcher :)

So now, we need to run php app/console router:dump-apache -e=prod --no-debug in order to generate the url_rewrite rules. Copy them inside web/.htaccess instead of the default rule:

<IfModule mod_rewrite.c>
    RewriteEngine On

    #<IfModule mod_vhost_alias.c>
    #    RewriteBase /
    #</IfModule>

    # You can remove those 2 lines!
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^(.*)$ app.php [QSA,L]

    # Copy here the output of the previous command
</IfModule>

This should be done each time a route is added/modified/deleted but as said previously, in most cases it will work anyway because of inheritance.

That's it!

Now to save a little more processing, change in web/app.php occurences of Request into ApacheRequest:

<?php

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

use Symfony\Component\HttpFoundation\ApacheRequest;

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = ApacheRequest::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

I could try to open a PR for it but I'm afraid it wouldn't be very understandable so if anyone else wants to do it, please do :)

/cc @weaverryan

Contributor

jalliot commented Apr 22, 2012

Ok so I've tested how to use the ApacheMatcher today so here it goes:
Add this to app/config/config_prod.yml:

parameters:
    router.options.matcher.cache_class: ~
    router.options.matcher_class: Symfony\Component\Routing\Matcher\ApacheUrlMatcher

The first option disables the generated router cache class which is of no use when using ApacheUrlMatcher and would bypass it...
The second one changes the default UrlMatcher to the Apache one.
A good thing to note is that ApacheUrlMatcher extends UrlMatcher so that even if you don't regenerate the url_rewrite rules, it will work (because at the end of ApacheUrlMatcher::match a call to parent::match is done). But of course, when doing so we lose the advantage of this matcher :)

So now, we need to run php app/console router:dump-apache -e=prod --no-debug in order to generate the url_rewrite rules. Copy them inside web/.htaccess instead of the default rule:

<IfModule mod_rewrite.c>
    RewriteEngine On

    #<IfModule mod_vhost_alias.c>
    #    RewriteBase /
    #</IfModule>

    # You can remove those 2 lines!
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^(.*)$ app.php [QSA,L]

    # Copy here the output of the previous command
</IfModule>

This should be done each time a route is added/modified/deleted but as said previously, in most cases it will work anyway because of inheritance.

That's it!

Now to save a little more processing, change in web/app.php occurences of Request into ApacheRequest:

<?php

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

use Symfony\Component\HttpFoundation\ApacheRequest;

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = ApacheRequest::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

I could try to open a PR for it but I'm afraid it wouldn't be very understandable so if anyone else wants to do it, please do :)

/cc @weaverryan

@weaverryan

This comment has been minimized.

Show comment Hide comment
@weaverryan

weaverryan Apr 28, 2012

Member

This is ready to go - someone just needs to take @jalliot's nice work and turn it into a quick cookbook article.

We need to make that article work for 2.0 first (so, for example, there is no $kernel->terminate in 2.0). Once that is merged, we can updated it on the master branch for 2.1.

Thanks!

Member

weaverryan commented Apr 28, 2012

This is ready to go - someone just needs to take @jalliot's nice work and turn it into a quick cookbook article.

We need to make that article work for 2.0 first (so, for example, there is no $kernel->terminate in 2.0). Once that is merged, we can updated it on the master branch for 2.1.

Thanks!

@Inoryy

This comment has been minimized.

Show comment Hide comment
@Inoryy

Inoryy May 4, 2012

Contributor

I'm working on it

Contributor

Inoryy commented May 4, 2012

I'm working on it

@wouterj

This comment has been minimized.

Show comment Hide comment
@wouterj

wouterj Nov 27, 2012

Member

@weaverryan this should be closed as #1347 is merged.

Member

wouterj commented Nov 27, 2012

@weaverryan this should be closed as #1347 is merged.

@weaverryan weaverryan closed this Nov 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment