php.htaccesslaravelurl-rewritinggetparameter

How Laravel handle url-rewriting?


Just for curiosity... How Laravel framework captures "get" parameters?

I just opened their .htaccess and found the content bellow:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Note that in this part, there is no parameter being passed to index.php at all

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

The most common scenario would be something like:

RewriteRule ^(.*)$ index.php?getparams=$1 [L]

Solution

  • This is not Laravel specific, but how any PHP framework handles it.

    First, there's no need to add a query string to index.php, because it is already there implicitly. See RewriteRule

    ...
    Modifying the Query String

    By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

    @iainn already mentioned the superglobals $_GET and $_POST. There's also $_SERVER, which contains - among many others - REQUEST_URI or PATH_INFO. With these, you can get at the requested URL without passing it explicitly as part of the query string.