从joomla组件router.php中检索值

时间:2013-05-06 17:48:58

标签: php joomla router sef

好吧我有我的自定义组件路由器,但我仍然需要其中的值,我不知道如何在我的组件中使用这些值

例如在网址

的index.php选项= com_maodeobras&安培;查看= maodeobras&安培;猫= mymaincat&安培;分= mysubcat

我想要所需的结果(通过我的路由器实现)

maodeobras / mymaincat / mysubcat.html

问题是:

如何从组件或网址中检索变量cat和sub?

这是我的路由器

function MaodeobrasBuildRoute(&$query)
{
    $segments = array();

    if(isset($query['view'])):
        $segments[] = $query['view'];
        unset($query['view']);
    endif;

    if(isset($query['id'])):
        $segments[] = $query['id'];
        unset($query['id']);
    endif;

    if(isset($query['cat'])):
        $segments[] = $query['cat'];
        unset($query['cat']);
    endif;

    if(isset($query['sub'])):
        $segments[] = $query['sub'];
        unset($query['sub']);
    endif;

    return $segments;
}

function MaodeobrasParseRoute($segments)
{
    $vars = array();

   $app =& JFactory::getApplication();
   $menu =& $app->getMenu();
   $item =& $menu->getActive();
    // Count segments
    $count = count($segments);
    //Handle View and Identifier
    switch($segments[0])
    {
        case 'maodeobras':
            //if ($count == 1) {
                $id = explode(':', $segments[$count-1]);
                $vars['id'] = (int) $id[0];
                $vars['view'] = 'maodeobras';
            //}
            break;

        case 'categoriasdemãodeobras':
            $id = explode(':', $segments[$count-1]);
            $vars['id'] = (int) $id[0];
            $vars['view'] = 'categoriasdemãodeobras';
            break;
        case 'madeobrasfiltros':
            $id = explode(':', $segments[$count-1]);
            $vars['id'] = (int) $id[0];
            $vars['view'] = 'madeobrasfiltros';
            break;

        case 'subcategorias':
            $id = explode(':', $segments[$count-1]);
            $vars['id'] = (int) $id[0];
            $vars['view'] = 'subcategorias';
            break;
    }

    return $vars;
}

1 个答案:

答案 0 :(得分:2)

我让它工作,忘记组件构建者给你的所有垃圾,并创建你自己的router.php

继承人的解决方案! 查询:

的index.php选项= com_maodeobras&安培;猫= mymaincat&安培;分= mysubcat&安培; ITEMID = 123

你会喜欢这样的自己的网址 /mymaincat/mysubcat.html

function MaodeobrasBuildRoute(&$query)
{
    $segments = array();

    if(isset($query['cat'])):
        $segments[] = $query['cat'];
        unset($query['cat']);
    endif;

    if(isset($query['sub'])):
        $segments[] = $query['sub'];
        unset($query['sub']);
    endif;

    return $segments;
}

function MaodeobrasParseRoute($segments)
{
$vars = array();

   $vars['cat'] = $segments[0];
   $vars['sub'] = $segments[1];

    return $vars;
}

要在视图文件中检索这些值,请执行此操作 echo JRequest :: getVar('cat'); 要么 echo JRequest :: getVar('sub');

多数人全部!!