diff options
author | emkael <emkael@tlen.pl> | 2016-02-26 17:13:13 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-02-26 17:18:02 +0100 |
commit | d327af5f481454d8738ab413e2c16ad829bbf800 (patch) | |
tree | 8a1d06af2e70132077a6c08bf4823ecbb27d8fbf | |
parent | 0f69294121ee68616961af02bcfc0c7662cd3ba5 (diff) |
* friendly URL support
-rw-r--r-- | app/php/application.xml | 34 | ||||
-rw-r--r-- | app/php/url/UrlManager.php | 57 | ||||
-rw-r--r-- | http/.htaccess | 10 |
3 files changed, 97 insertions, 4 deletions
diff --git a/app/php/application.xml b/app/php/application.xml index 4aa2f30..13a86e1 100644 --- a/app/php/application.xml +++ b/app/php/application.xml @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <application id="http" mode="Debug"> <modules> + <module id="db" class="Application.db.DBModule" config="../../config/db.json" /> @@ -9,9 +10,34 @@ EnableCache="true" /> <module id="cache" class="System.Caching.TDbCache" /> - <!-- Remove this comment mark to enable PATH url format - <module id="request" class="THttpRequest" UrlFormat="Path" /> - --> + + <module id="request" + class="THttpRequest" + UrlFormat="HiddenPath" + UrlParamSeparator="/" + UrlManager="url" /> + <module id="url" + class="Application.url.UrlManager" + UrlPrefix="/" + EnableCustomUrl="True"> + <url ServiceParameter="Home" + UrlFormat="HiddenPath" + pattern="{month}/{year}/" + parameters.month="\d{2}" + parameters.year="\d{4}" /> + <url ServiceParameter="Home" + UrlFormat="HiddenPath" + pattern="{month}/" + parameters.month="\d{2}" /> + <url ServiceParameter="Home" + UrlFormat="HiddenPath" + EnableCustomUrl="false" + pattern="" /> + <url ServiceParameter="*" + UrlFormat="HiddenPath" + EnableCustomUrl="false" + pattern="{*}" /> + </module> <!-- Remove this comment mark to enable logging <module id="log" class="System.Util.TLogRouter"> <route class="TBrowserLogRoute" Categories="System" /> @@ -19,6 +45,6 @@ --> </modules> <services> - <service id="page" class="TPageService" DefaultPage="Home" /> + <service id="page" class="TPageService" /> </services> </application> diff --git a/app/php/url/UrlManager.php b/app/php/url/UrlManager.php new file mode 100644 index 0000000..a33d98e --- /dev/null +++ b/app/php/url/UrlManager.php @@ -0,0 +1,57 @@ +<?php + +Prado::using('System.Web.TUrlMapping'); + +class UrlManager extends TUrlMapping { + + public function constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems) { + $url = parent::constructUrl( + $serviceID, + $serviceParam, + $getItems, + $encodeAmpersand, + $encodeGetItems + ); + return rtrim( + preg_replace( + '#^/' . $serviceParam . '#', + '/' . $this->_convertServiceParam($serviceParam), + preg_replace('#^/' . $serviceID . '#', '', $url) + ), + '/' + ) . '/'; + } + + public function parseUrl() { + $params = parent::parseUrl(); + if ($this->MatchingPattern) { + $serviceID = $this->MatchingPattern->ServiceID; + if (isset($params[$serviceID])) { + $params[$serviceID] = $this->_parseServiceParam($params[$serviceID]); + } + } + return $params; + } + + /** + * Convert service param from camelCase to hyphenated-form. + **/ + private function _convertServiceParam($param) { + return implode( + '-', + array_map('mb_strtolower', array_filter(preg_split('/(?=[A-Z])/', $param))) + ); + } + + /** + * Convert service param from hyphenated-form to camelCase. + **/ + private function _parseServiceParam($param) { + return implode( + '', + array_map('ucfirst', explode('-', $param)) + ); + } +} + +?> diff --git a/http/.htaccess b/http/.htaccess new file mode 100644 index 0000000..77ee952 --- /dev/null +++ b/http/.htaccess @@ -0,0 +1,10 @@ +RewriteEngine On + +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*[^/])$ /$1/ [L,R=301] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)$ /index.php/$1/ [L] + |