【问题标题】:joomla - router change url when getting the name of productjoomla - 获取产品名称时路由器更改网址
【发布时间】:2017-05-15 07:21:53
【问题描述】:

我已经在 joomla 中构建了我自己的组件,客户现在想要一个友好的网址 f.e website.com/someplace/{product-id}-{product-name}。所以我像这样构建自己的路由器。

function componentBuildRoute(&$query)
{
$segments = [];

if (isset($query['view'])) {
    $segments[] = "szkolenie";
    unset($query['view']);
}

if (isset($query['product_id'])) {
    $productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
    $newName = $query['product_id'] . '-' . $productName;
    $segments[] = $newName;

    unset($query['product_id']);
}
return $segments;
}

并解析路由函数

function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);

switch ($item->query['view']) {
    case 'catalogue' : {
        $view = 'training';
        $id = $segments[1];
    }
        break;
}

$data = [
    'view' => $view,
    'product_id' => $id
];
return $data;
}

虽然在 buildroute 函数段的末尾还可以,但我有我想要的,在解析路由的开头我有类似的东西 website.com/szkolenie/1-krakow

function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
    ->from($db->quoteName('#__component_training'))
    ->where('#__s4edu_product.product_id = ' . $productId)
    ->leftJoin('#__component_product ON 
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}

考虑到这一切,我认为 buildRoute 和 parseRoute 之间发生了一些事情,过滤了 $segment[1] 变量,但是如何禁用它以及为什么会发生?

附言 请不要发给我https://docs.joomla.org/Joomla_Routes_%26_SEF 我已经知道 joomla 网站上的所有教程,其中包含任何带有 sef 的内容。

附言

它基于 joomla 3.7.0 构建

【问题讨论】:

    标签: joomla


    【解决方案1】:

    您没有名为“krakow”的产品?

    如果没有,您可以尝试从构建函数中删除 $productName,以检查此“krakow”是自动添加的还是来自 getProductName() 函数。

    我还注意到你在函数 getProductName() 中有一个错误

    ->where('#__s4edu_product.product_id = ' . $productId)
    

    应该是

    ->where('#__component_product.product_id = ' . $productId)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      相关资源
      最近更新 更多