1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-11 00:54:49 +02:00

Better routing/URL assembling, inline documentation, large number of fixes, news area improvements - should work flowless with every URL configuration. Awaiting testing results for more stability fixes.

This commit is contained in:
secretr
2011-11-30 15:14:02 +00:00
parent 076f03b696
commit 078816befe
12 changed files with 299 additions and 94 deletions

View File

@@ -134,7 +134,16 @@ function nextprev_shortcode($parm = '')
if($total_pages <= 1) { return ''; }
// urldecoded once by parse_str()
$url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $parm['url']);
if(substr($parm['url'], 0, 5) == 'url::')
{
// New - use URL assembling engine
// Format is: url::route::params::options
// Example: url::news/list/category::id=xxx&name=yyy&page=--PAGE--::full=1
// WARNING - url parameter string have to be rawurlencode-ed BEFORE passed to the shortcode, or it'll break everything
$urlParms = explode('::', $parm['url']);
$url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $e107->url->create($urlParms[1], $urlParms[2], varset($urlParms[3])));
}
else $url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $parm['url']);
// Simple parser vars
$e_vars = new e_vars(array(

View File

@@ -1,7 +1,12 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Default config - create ONLY - old legacy URLs
* All possible config options added here - to be used as a reference.
* A good programming practice is to remove all non-used options.
*/
class core_news_url extends eUrlConfig
{
@@ -9,17 +14,47 @@ class core_news_url extends eUrlConfig
{
return array(
'config' => array(
'allowMain' => false, // [optional] default false; disallow this module (while using this config) to be set as site main URL namespace
'noSingleEntry' => true, // [optional] default false; disallow this module to be shown via single entry point when this config is used
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script
'format' => 'get', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing
'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating
'defaultRoute' => '', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'defaultRoute' => 'list/new',// [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html)
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html), not used when format is 'get' or legacy non-empty
),
'rules' => array() // rule set array
'rules' => array(), // rule set array - can't be used with format 'get' and noSingleEntry true
### [optional] vars mapping (create URL routine), override per rule is allowed
### Keys of this array will be used as a map for finding values from the provided parameters array.
### Those values will be assigned to new keys - corresponding values of mapVars array
### It gives extremely flexibility when used with allowVars. For example we pass $news item array as
### it's retrieved from the DB, with no modifications. This gives us the freedom to create any variations of news
### URLs using the DB data with a single line URL rule. Another aspect of this feature is the simplified code
### for URL assembling - we just do eRouter::create($theRoute, $newsDbArray)
### Not used when in selfCreate mod (create url)
'mapVars' => array(
//'news_id' => 'id',
//'news_sef' => 'name',
),
### [optional] allowed vars definition (create URL routine), override per rule is allowed
### This numerical array serves as a filter for passed vars when creating URLs
### Everything outside this scope is ignored while assembling URLs. Exception are route variables.
### For example: when <id:[\d]+> is present in the route string, there is no need to extra allow 'id'
### To disallow everything but route variables, set allowVars to false
### When format is get, false value will disallow everything (no params) and default preserved variables
### will be extracted from mapVars (if available)
### Default value is empty array
### Not used when in selfCreate mod (create url)
'allowVars' => array(/*'page', 'name'*/),
### Those are regex templates, allowing us to avoid the repeating regex patterns writing in your rules.
### varTemplates are merged with the core predefined templates. Full list with core regex templates and examples can be found
### in rewrite_extended news URL config
'varTemplates' => array(/*'testIt' => '[\d]+'*/),
);
}
@@ -87,20 +122,9 @@ class core_news_url extends eUrlConfig
case 'day':
case 'month':
case 'year':
$url .= $route[1].'-'.$params['id'];
break;
case 'nextprev':
$route = $params['route'];
unset($params['route']);
if($route != 'list/nextprev')
{
$params['page'] = '[FROM]';
$url = $this->create($route, $params);
unset($tmp);
}
break;
if($page) $page = '.'.$page;
$url .= $route[1].'.'.$params['id'].$page;
break;
default:
$url = 'news.php';

View File

@@ -1,7 +1,10 @@
<?php
/**
* Mod rewrite & SEF URLs support, manually (rules-less) created/parsed urls
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Mod rewrite & SEF URLs support, example of manually (rules-less) created/parsed urls
*/
class core_news_rewrite_url extends eUrlConfig
{
@@ -9,17 +12,15 @@ class core_news_rewrite_url extends eUrlConfig
{
return array(
'config' => array(
'allowMain' => true,
'noSingleEntry' => false, // [optional] default false; disallow this module to be shown via single entry point when this config is used
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing
'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating
'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html)
'urlSuffix' => '.html', // [optional] default empty; string to append to the URL (e.g. .html)
),
'rules' => array() // rule set array
);
}
@@ -42,7 +43,7 @@ class core_news_rewrite_url extends eUrlConfig
if('--FROM--' != $params['page']) $page = $params['page'] ? intval($params['page']) : '0';
else $page = '--FROM--';
if(!$route) $route = 'item/default';
if(!$route) $route = 'list/items';
if(is_string($route)) $route = explode('/', $route, 2);
$r = array();
@@ -88,7 +89,7 @@ class core_news_rewrite_url extends eUrlConfig
// news/Category/Category-Name?page=xxx
// news/Short/Category-Name?page=xxx
$r[0] = $route[1] == 'category' ? 'Short' : 'Category';
$r[1] = $params['id'];
$r[1] = $params['name'] ? $params['name'] : $params['id'];
if($page) $parm = array('page' => $page);
}
break;
@@ -96,7 +97,8 @@ class core_news_rewrite_url extends eUrlConfig
case 'day':
case 'month':
case 'year':
$r[0] = $route[1].'-'.$params['id'];
$r = array($route[1], intval($params['id']));
if($page) $parm = array('page' => $page);
break;
default:
@@ -179,7 +181,8 @@ class core_news_rewrite_url extends eUrlConfig
if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]);
$this->legacyQueryString = 'day-'.$id;
$this->legacyQueryString = 'day.'.$id.'.'.$page;
return 'list/day';
break;
# could be pref or LAN constant
@@ -187,15 +190,17 @@ class core_news_rewrite_url extends eUrlConfig
if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]);
$this->legacyQueryString = 'month-'.$id;
$this->legacyQueryString = 'month.'.$id.'.'.$page;
return 'list/month';
break;
# could be pref or LAN constant
# could be pref or LAN constant - not supported yet
case 'year':
if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]);
$this->legacyQueryString = 'year-'.$id;
$this->legacyQueryString = 'year.'.$id.'.'.$page;
//return 'list/year';
break;
# force not found

View File

@@ -1,7 +1,13 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Mod rewrite & SEF URLs support, managed entirely by the core router (rules)
* It contains a lot of examples (mostly complex), use them to play around and learn things :/
* Generally, things are much more simpler...
*
*/
class core_news_rewrite_extended_url extends eUrlConfig
{
@@ -12,7 +18,8 @@ class core_news_rewrite_extended_url extends eUrlConfig
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script; override per rule is allowed
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'legacyQuery' => '', // [optional] default null; default legacy query string template, parsed (simpleParse) with requestParams values (request object) and GET vars part of allowVars array (rule); override per rule is allowed
'urlSuffix' => '',
'allowMain' => true,
### default vars mapping (create URL), override per rule is allowed
'mapVars' => array(
@@ -24,6 +31,28 @@ class core_news_rewrite_extended_url extends eUrlConfig
### false means - disallow all vars beside those required by the rules
### Override per rule is allowed
'allowVars' => false,
### Best news - you don't need to write one and the same
### regex over and over again. Even better news - you might avoid
### writing regex at all! Just use the core regex templates, they
### should fit almost every case.
### Here is a test custom regex template:
'varTemplates' => array('testIt' => '[\d]+'),
/* Predefined Core regex templates, see usage below
'az' => '[A-Za-z]+', // NOTE - it won't match non-latin word characters!
'alphanum' => '[\w\pL]+',
'sefsecure' => '[\w\pL.\-\s!,]+',
'secure' => '[^\/\'"\\<%]+',
'number' => '[\d]+',
'username' => '[\w\pL.\-\s!,]+',
'azOptional' => '[A-Za-z]{0,}',
'alphanumOptional' => '[\w\pL]{0,}',
'sefsecureOptional' => '[\w\pL.\-\s!,]{0,}',
'secureOptional' => '[^\/\'"\\<%]{0,}',
'numberOptional' => '[\d]{0,}',
'usernameOptional' => '[\w\pL.\-\s!,]{0,}',
*/
),
'rules' => array(
@@ -31,36 +60,41 @@ class core_news_rewrite_extended_url extends eUrlConfig
'' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
'Category' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
## URL with ID and Title - no DB call, balanced performance!
'Category/<id:[\d]+>/<name:[\w\pL.\-\s]+>' => array('list/items', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
## URL with ID and Title - no DB call, balanced performance, name optional
## Demonstrating the usage of custom user defined regex template defined above - 'testIt'
'Category/<id:{testIt}>/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
## URL with ID only - best performance!
// 'Category/<id:[\d]+>' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'list.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
## URL with Title only - prettiest and slowest!
##'Category/<name:[\w\pL.\-\s]+>' => array('list/items', 'allowVars' => array('page'), 'mapVars' => array('category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}', 'parseCallback' => 'categoryIdByTitle'),
## URL with Title only - prettiest and slowest! Example with direct regex - no templates
//'Category/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_name' => 'name'), 'legacyQuery' => 'list.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
## URL with ID only - best performance, fallback when no sef name provided
'Category/<id:{number}>' => array('list/category', 'allowVars' => array('page'), 'legacyQuery' => 'list.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
### View item requested by id or string, if you remove the catch ALL example, uncomment at least on row from this block
### leading category name example - could be enabled together with the next example to handle creating of URLs without knowing the category title
// 'View/<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// to be noted here - value 'name' is replaced by item id within the callback method; TODO replace news_title with news_sef field
// 'View/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// 'View/<id:[\d]+>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
// 'View/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// 'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
## URL with ID and Title - no DB call, balanced performance!
'Short/<id:[\d]+>/<name:[\w\pL.\-\s]+>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
'Short/<id:{number}>/<name:{sefsecure}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'cat.{id}.{page}'),
## fallback when name is not provided
'Short/<id:{number}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id'), 'legacyQuery' => 'cat.{id}.{page}'),
// less used after
'Brief/<id:[\d]+>' => array('list/short', 'allowVars' => array('page'), 'legacyQuery' => 'cat.{id}.{page}', 'mapVars' => array('news_id' => 'id')),
'Day/<id:[\d]+>' => array('list/day', 'legacyQuery' => 'day-{id}'),
'Month/<id:[\d]+>' => array('list/month', 'legacyQuery' => 'month-{id}'),
'Year/<id:[\d]+>' => array('list/year', 'legacyQuery' => 'year-{id}'),
//'Brief/<id:[\d]+>' => array('list/short', 'allowVars' => array('page'), 'legacyQuery' => 'cat.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
'Day/<id:{number}>' => array('list/day', 'allowVars' => array('page'), 'legacyQuery' => 'day.{id}.{page}'),
'Month/<id:{number}>' => array('list/month', 'allowVars' => array('page'), 'legacyQuery' => 'month.{id}.{page}'),
//'Year/<id:[\d]+>' => array('list/year', 'allowVars' => array('page'), 'legacyQuery' => 'year.{id}.{page}'), not supported yet
### View news item - kinda catch all - very bad performance when News is chosen as default namespace - two additional DB queries on every site call!
// Leading category name - uncomment to enable
'<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// Base location as item view - uncomment to enable
// '<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
## Leading category name - uncomment to enable
'<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'mapVars' => array('category_name' => 'category', 'news_title' => 'name', 'news_id' => 'id'), 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// Base location as item view - fallback if category sef is missing
'<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'mapVars' => array('news_id' => 'id', 'news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// fallback if news sef is missing
'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
)
);
@@ -109,15 +143,25 @@ class core_news_rewrite_extended_url extends eUrlConfig
public function itemIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(!$name || is_numeric($name)) return;
if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url');
$name = e107::getParser()->toDB($name);
if($sql->db_Select('news', 'news_id', "news_title='{$name}'")) // TODO - it'll be news_url (new) field
if($sql->db_Select('news', 'news_id', "news_title='{$name}'")) // TODO - it'll be news_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['news_id']);
}
else $request->setRequestParam('name', 0);
}
/**
@@ -127,14 +171,24 @@ class core_news_rewrite_extended_url extends eUrlConfig
public function categoryIdByTitle(eRequest $request)
{
$name = $request->getRequestParam('name');
if(!$name || is_numeric($name)) return;
if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url');
$id = e107::getParser()->toDB($name);
if($sql->db_Select('news_category', 'category_id', "category_name='{$name}'")) // TODO - it'll be category_url (new) field
if($sql->db_Select('news_category', 'category_id', "category_name='{$name}'")) // TODO - it'll be category_sef (new) field
{
$name = $sql->db_Fetch();
$request->setRequestParam('name', $name['category_id']);
}
else $request->setRequestParam('name', 0);
}
}

View File

@@ -1,5 +1,10 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* Search routing config
*/
class core_search_url extends eUrlConfig
{
public function config()

View File

@@ -1,5 +1,10 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* Search routing config
*/
class core_search_rewrite_url extends eUrlConfig
{
public function config()

View File

@@ -1,5 +1,10 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* System routing config
*/
class core_system_rewrite_url extends eUrlConfig
{
public function config()
@@ -7,14 +12,17 @@ class core_system_rewrite_url extends eUrlConfig
return array(
'config' => array(
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'defaultRoute' => 'error/notfound', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'allowMain' => true,
'format' => 'path',
'defaultRoute' => 'error/notfound',
'errorRoute' => 'error/notfound',
),
// rule set array
'rules' => array(
'error404' => 'error/notfound',
'error404' => 'error/notfound',
'hello' => 'error/hello-world',
)
);
}

View File

@@ -1,5 +1,10 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* User routing config
*/
class core_user_url extends eUrlConfig
{
public function config()

View File

@@ -1,5 +1,10 @@
<?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* User routing config
*/
class core_user_rewrite_url extends eUrlConfig
{
public function config()