Last active
January 1, 2016 09:59
-
-
Save asika32764/8128928 to your computer and use it in GitHub Desktop.
分成 REST 形式與 User Friendly (Alias) 形式的兩種寫法: Put it at: {FRONT-END}/components/com_flower/router.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Site | |
* @subpackage com_flower | |
* @author Simon ASika <asika32764@gmail.com> | |
* @copyright Copyright (C) 2013 Asikart. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
// No direct access | |
defined('_JEXEC') or die; | |
/** | |
* @param array A named array | |
* | |
* @return array | |
*/ | |
function FlowerBuildRoute(&$query) | |
{ | |
$segments = array(); | |
$params = array( | |
'view', | |
'layout', | |
'id' | |
); | |
// use rest uri | |
// ================================================================================================ | |
foreach ($params as $param): | |
if (isset($query[$param])) | |
{ | |
$segments[] = $query[$param]; | |
unset($query[$param]); | |
} | |
endforeach; | |
return $segments; | |
} | |
/** | |
* @param array A named array | |
* @param array | |
* Formats: | |
* index.php?/view/layout/id | |
* index.php?/view/id | |
*/ | |
function FlowerParseRoute($segments) | |
{ | |
$vars = array(); | |
// view is always the first element of the array | |
$count = count($segments); | |
$params = array( | |
'view', | |
'layout', | |
'id' | |
); | |
foreach ($params as $param): | |
if ($count) | |
{ | |
$count--; | |
$segment = array_shift($segments); | |
if (is_numeric($segment)) | |
{ | |
$vars['id'] = $segment; | |
} | |
elseif ($segment) | |
{ | |
$vars[$param] = $segment; | |
} | |
} | |
endforeach; | |
return $vars; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Site | |
* @subpackage com_flower | |
* @author Simon ASika <asika32764@gmail.com> | |
* @copyright Copyright (C) 2013 Asikart. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
// No direct access | |
defined('_JEXEC') or die; | |
/** | |
* @param array A named array | |
* | |
* @return array | |
*/ | |
function FlowerBuildRoute(&$query) | |
{ | |
$segments = array(); | |
$params = array( | |
'view', | |
'layout', | |
'id' | |
); | |
// User friendly URI begin | |
// ================================================================================================ | |
// Convert: index.php?option=com_flower&view=sakura&id=50&alias=sakura-item-name | |
// To: /category/50-sakura-item-name | |
// get a menu item based on Itemid or currently active | |
// ================================================================================================ | |
$app = JFactory::getApplication(); | |
$menu = $app->getMenu(); | |
// set Menu Itemid | |
// ================================================================================================ | |
if (empty($query['Itemid'])) | |
{ | |
$menuItem = $menu->getActive(); | |
$menuItemGiven = false; | |
} | |
else | |
{ | |
$menuItem = $menu->getItem($query['Itemid']); | |
$menuItemGiven = true; | |
} | |
$com_menu = $menu->getItems( array('component'), array('com_flower') ); | |
// Query item menu, if there has a menu, use menu URI first | |
// Example: /item-alias | |
// ================================================================================================ | |
$item_menu = null ; | |
foreach( $com_menu as $menuitem ): | |
if(!isset($menuitem->query['id']) || !isset($query['id']) ) | |
{ | |
continue ; | |
} | |
if( $menuitem->query['view'] == 'sakura' && $menuitem->query['id'] == $query['id'] ) | |
{ | |
$item_menu = $menuitem ; | |
//$segments = explode( '/', $item_menu->route); | |
unset($query['id']); | |
unset($query['view']); | |
unset($query['catid']) ; | |
unset($query['alias']) ; | |
$query['Itemid'] = $item_menu->id ; | |
return $segments ; | |
} | |
endforeach; | |
// Query item in category menu, if item in category menu, use menu URI | |
// Example: /category/50-item-alias | |
// ================================================================================================ | |
$item_menu = null ; | |
foreach( $com_menu as $menuitem ): | |
if(!isset($menuitem->query['id']) || !isset($query['catid']) ) | |
{ | |
continue ; | |
} | |
if( $menuitem->query['view'] == 'sakuras' && $menuitem->query['id'] == $query['catid'] ) | |
{ | |
$item_menu = $menuitem ; | |
//$segments = explode( '/', $item_menu->route); | |
$last = $query['id'] ; | |
$last .= isset($query['alias']) ? '-' . $query['alias'] : '' ; | |
$segments[] = $last ; | |
unset($query['id']); | |
unset($query['view']); | |
unset($query['catid']) ; | |
unset($query['alias']) ; | |
$query['Itemid'] = $item_menu->id ; | |
return $segments ; | |
} | |
endforeach; | |
// Query category menu, if is sakuras and has category menu, use menu URI | |
// Example: /category | |
// ================================================================================================ | |
$cat_menu = null ; | |
foreach( $com_menu as $menuitem ): | |
if(!isset($menuitem->query['id']) || !isset($query['id']) ) | |
{ | |
continue ; | |
} | |
if( $menuitem->query['view'] == 'sakuras' && $menuitem->query['id'] == $query['id'] ) | |
{ | |
$cat_menu = $menuitem ; | |
unset($query['view']); | |
unset($query['id']); | |
$query['Itemid'] = $cat_menu->id ; | |
return $segments ; | |
} | |
endforeach; | |
// ================================================================================================ | |
// User friendly URI end | |
// No menu match, use rest uri | |
// ================================================================================================ | |
foreach ($params as $param): | |
if (isset($query[$param])) | |
{ | |
$segments[] = $query[$param]; | |
unset($query[$param]); | |
} | |
endforeach; | |
// Unset needless query | |
// ================================================================================================ | |
unset($query['catid']); | |
unset($query['alias']); | |
return $segments; | |
} | |
/** | |
* @param array A named array | |
* @param array | |
* Formats: | |
* index.php?/item (item in menu) | |
* index.php?/category/xx-item (item in category menu) | |
* index.php?/categories (category menu) | |
*/ | |
function FlowerParseRoute($segments) | |
{ | |
$vars = array(); | |
// view is always the first element of the array | |
$count = count($segments); | |
$params = array( | |
'view', | |
'layout', | |
'id' | |
); | |
// User friendly URI begin | |
// ================================================================================================ | |
$last_seg = array_pop($segments); | |
if(!is_numeric($last_seg)) | |
{ | |
// Joomla will auto convert '-' to ':', so we use ':' to separate alias and id. | |
$last_seg = explode(':', $last_seg); | |
$id = $last_seg[0]; | |
if(is_numeric($id)) | |
{ | |
$segments[] = $id ; | |
$vars['view'] = 'sakura' ; | |
} | |
} | |
else | |
{ | |
$segments[] = $last_seg ; | |
} | |
// ================================================================================================ | |
// User friendly URI end | |
// No menu found, use REST style | |
foreach ($params as $param): | |
if ($count) | |
{ | |
$count--; | |
$segment = array_shift($segments); | |
if (is_numeric($segment)) | |
{ | |
$vars['id'] = $segment; | |
} | |
elseif ($segment) | |
{ | |
$vars[$param] = $segment; | |
} | |
} | |
endforeach; | |
return $vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment