具有类别/特色的自定义组件和router.php

时间:2019-07-09 16:32:19

标签: php joomla

我正在使用Joomla 3.9 我已经开发了一个自定义组件。 在我的组件中,此路由器代码有效。 该代码适用于:

  1. 表单搜索

/ myform / first_speciality_alias

/ myform / second_speciality_alias

  1. 列表视图

/ myform / first_speciality_alias

/ myform / second_speciality_alias

  1. 详细信息视图

/ myform / first_speciality_alias / third_reference

/ myform / second_speciality_alias / fourth_reference

  1. 通过直接URL访问

此路由器代码在做什么? 他将我的表单字段之一与URL同步。

问题是,我还需要特殊菜单项。 但是,一旦我创建了特色菜单项;

  1. 情况1确实: / myform / first_speciality_alias / first_speciality_alias
  2. 一旦我单击菜单中存在的first_speciality_alias,URLS就会变为 / myform / first_speciality_alias / first_speciality_alias / myform / first_speciality_alias / second_speciality_alias <=当我单击表单中的second_speciality_alias
  3. 详细信息视图不起作用,它显示列表视图。
  4. 从直接URL访问似乎可以工作1/2次。

mycomponent / router.php

<?php
defined('_JEXEC') or die;
class MyComponentRouter extends JComponentRouterView
{   
    public function build(&$query)
    {
        $segments = array();
        if (isset($query['idspeciality']))
        {
            $db = JFactory::getDbo();
            $qry = $db->getQuery(true);
            $qry->select('alias');
            $qry->from('speciality');
            $qry->where('id = ' . $db->quote($query['idspeciality']));
            $db->setQuery($qry);
            $alias = $db->loadResult();
            $segments[] = $alias;
            unset($query['idspeciality']);
        }
        if (isset($query['reference']))
        {
            $segments[] = $query['reference'];
            unset($query['reference']);
        }
        unset($query['view']);
        return $segments;
    }

    public function parse(&$segments)
    {
        $vars = array();

        $db = JFactory::getDbo();
        $qry = $db->getQuery(true);
        $qry->select('id');
        $qry->from('speciality');
        $qry->where('alias = ' . $db->quote($segments[0]));
        $db->setQuery($qry);
        $id = $db->loadResult();

        if(!empty($id))
        {
            $vars['idspeciality'] = $id;
            $vars['view'] = 'mylistview';
        }
        if(!empty($segments[1]))
        {
            $vars['reference'] = $segments[1];
            $vars['view'] = 'mydetailsview';
        }

        return $vars;
    }
}

mycomponent / views / mylistview / tmpl / view.html.php

<?php
defined('_JEXEC') or die('Restricted access');
class MyComponentViewMyComponent extends JViewLegacy {
    function display($tpl = null) {

        ...

        $speciality_from_url = JRequest::getVar('idspeciality');
        # If POST speciality sent I rewrite to the correct URL
        if(isset($_POST['secteur'])){
            $session->set('idspeciality', $_POST['secteur']);
            $url = JRoute::_('index.php?option=com_mycomponent&view=mylistview'.'&idspeciality=' . $session->get('idspeciality'), false);
            $app->redirect($url);
            return false;
        }
        elseif($speciality_from_url != $session->get('idspeciality')){
            $session->set('idspeciality', $speciality_from_url);
            $url = JRoute::_('index.php?option=com_mycomponent&view=mylistview'.'&idspeciality=' . $session->get('idspeciality'), false);
            $app->redirect($url);
            return false;
        }

        ...

    }
}

mycomponent / views / mylistview / tmpl / default.xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="List view">
        <message>List view.</message>
    </layout>
            <fields
            name="request"
                        addfieldpath="/administrator/components/com_mycomponent/models/fields"
            >
        <fieldset name="request">
            <field
                    name="idspeciality"
                    type="customtype"
                    label="Speciality"
                    description="Sort list view"
                    >
                <option value="">Pick one</option>
            </field>
        </fieldset>
    </fields>

</metadata>

0 个答案:

没有答案