dqnz43863 2013-05-06 17:48
浏览 31
已采纳

从joomla组件router.php中检索值

ok i got my custom component router, but i still need the values inside it and i dont know how can i use these values in my component

for example in the url

index.php?option=com_maodeobras&view=maodeobras&cat=mymaincat&sub=mysubcat

i want the desired result (achieved with my router)

maodeobras/mymaincat/mysubcat.html

the question is:

how can i retrieve the variables cat and sub from the component or url?

here's my router

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条回答 默认 最新

  • doudula1974 2013-05-07 14:03
    关注

    I got it working, forget about all the trash that the component builders give u, and create your own router.php

    heres the solution! for a query:

    index.php?option=com_maodeobras&cat=mymaincat&sub=mysubcat&Itemid=123

    u gonna have a sef url like /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;
    }
    

    And to retrieve these values inside your view file, simple do this echo JRequest::getVar('cat'); or echo JRequest::getVar('sub');

    Thats All!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 修改光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解