Jump to content

Ocs-server/Gfx4/URL Rewriting: Difference between revisions

From KDE Community Wiki
Snizzo (talk | contribs)
Created page with "Gfx contains a simple but working url rewriter that works in conjunction with the MVC system. It basically translates an URL asked by the client into a new one elaborated by t..."
 
Snizzo (talk | contribs)
No edit summary
 
Line 2: Line 2:


All the rules are in the file ''config/rewrite.conf.php''. This is an example rewrite config file:
All the rules are in the file ''config/rewrite.conf.php''. This is an example rewrite config file:
 
<syntaxhighlight lang="php">
     <?php die(); ?>
     <?php die(); ?>
     /help|/help/index
     /help|/help/index
     /yourpath|/install/index
     /yourpath|/install/index
</syntaxhighlight>


Notice how the first URL is the one written by the client and the second one is that one elaborated by the server, when handling the request of a webpage.
Notice how the first URL is the one written by the client and the second one is that one elaborated by the server, when handling the request of a webpage.
Line 17: Line 18:
-------------------
-------------------
A little example:
A little example:
<syntaxhighlight lang="php">
    <?php die(); ?>
<?php die(); ?>
    /|/main/index|exact
/|/main/index|exact
    /help|/help/index|normal
/help|/help/index|normal
    /games|/games/lates|normal
/games|/games/lates|normal
</syntaxhighlight>


And then it will rewrite doing a simple string replace, but internally on the engine.
And then it will rewrite doing a simple string replace, but internally on the engine.
Line 29: Line 31:
If more rules are matching for the same url, the one with the longest key will be
If more rules are matching for the same url, the one with the longest key will be
used. Example:
used. Example:
 
<syntaxhighlight lang="php">
    <?php die(); ?>
<?php die(); ?>
    /help/games|/help/games/latest|normal
/help/games|/help/games/latest|normal
    /help|/help/index|normal
/help|/help/index|normal
 
</syntaxhighlight>
Only the first rule will be considered, even if it's defined before the second one.
Only the first rule will be considered, even if it's defined before the second one.


Line 40: Line 42:


Example of '''HelpController''':
Example of '''HelpController''':
    class HelpController extends EController {
<syntaxhighlight lang="php">
        public function index($args){
class HelpController extends EController {
            var_dump($args);
    public function index($args){
        }
        var_dump($args);
     }
     }
}
</syntaxhighlight>


Example of user browser opening the page ''www.example.com/help/index/arg1/arg2'':
Example of user browser opening the page ''www.example.com/help/index/arg1/arg2'':
Line 65: Line 69:


In order to build a nice title (also if it's not useful for your app logic you should use:
In order to build a nice title (also if it's not useful for your app logic you should use:
<syntaxhighlight lang="php">
    $url = ERewriter::prettify("I'm a terrible title");
$url = ERewriter::prettify("I'm a terrible title");
 
</syntaxhighlight>
Rewriting can be enabled/disabled via generic.conf.php:
Rewriting can be enabled/disabled via generic.conf.php:
    <?php die("You cannot see config in here."); ?>
<syntaxhighlight lang="php">
    rewrite|yes
<?php die("You cannot see config in here."); ?>
    rewrite|no
rewrite|yes
rewrite|no
</syntaxhighlight>

Latest revision as of 11:47, 28 July 2015

Gfx contains a simple but working url rewriter that works in conjunction with the MVC system. It basically translates an URL asked by the client into a new one elaborated by the server.

All the rules are in the file config/rewrite.conf.php. This is an example rewrite config file:

    <?php die(); ?>
    /help|/help/index
    /yourpath|/install/index

Notice how the first URL is the one written by the client and the second one is that one elaborated by the server, when handling the request of a webpage.

Everything that comes after a controller is usually handled as a parameter, except when you rewrite the URL. So, for example, calling /help/index/arg1/arg2/arg3 will be directly passed as an argument array to the method index($args) of the controller HelpController. Every other standard GET parameter can be passed as usual:

   http://www.example.com/help/index?x=5
   http://www.example.com/help/index/?x=5
   http://www.example.com/help/index/arg1/arg2?x=5
   http://www.example.com/help/index/arg1/arg2/?x=5

A little example:

<?php die(); ?>
/|/main/index|exact
/help|/help/index|normal
/games|/games/lates|normal

And then it will rewrite doing a simple string replace, but internally on the engine. So in this case /help will be rewritten internally as /help/index and therefore calling the index() method of the HelpController class defined.

If more rules are matching for the same url, the one with the longest key will be used. Example:

<?php die(); ?>
/help/games|/help/games/latest|normal
/help|/help/index|normal

Only the first rule will be considered, even if it's defined before the second one.

Every other parameter added after those slashes will be handled as parameter that will be passed to the controller's method via the $args array or can be retrieved by EController public methods. See Controller page for further documentation.

Example of HelpController:

class HelpController extends EController {
    public function index($args){
        var_dump($args);
    }
}

Example of user browser opening the page www.example.com/help/index/arg1/arg2:

   {
       "arg1",
       "arg2"
   }

Every other parameter passed as standard GET like ?var1=val1&var2=val2 is correctly set on EHeaderDataParser.

This kind of parameter management works also for rewritten URLs. For example it would exactly be the same if the user calls: www.example.com/help/arg1/arg2 without the "/index" part.

This is the "normal" rewrite.


The "exact" mode rewrites a URL only if it matches exactly so additional parameters via / aren't handled and accepted. Normal GET parameters are handled as usual.

In order to build a nice title (also if it's not useful for your app logic you should use:

	
$url = ERewriter::prettify("I'm a terrible title");

Rewriting can be enabled/disabled via generic.conf.php:

<?php die("You cannot see config in here."); ?>
rewrite|yes
rewrite|no