0

I am developing my second custom component for Joomla! 3.6.4 and I have another problem with my router.php. This time I need to dynamically change the view on my router.php but I don’t know how to do it. On my component, I have four different views, each one of them with a different name, of course. So, I have:

  1. View-1 list and View-1 single
  2. View-2 list and View-2 single
  3. View-3 list and View-3 single
  4. View-4 list and View-4 single

The problem with my router.php is that I don’t know how to dynamically change the view on the variable $vars['view'] = "VIEW";. The other problem is that I call the component’s links from a module outside a Menu Item, so it is all really complex. However, my question is: How can I pull the view dynamically and then populate the $vars['view']?

Here is my router.php:

class ComponentNameRouter extends JComponentRouterBase
{
    /**
     * Build method for URLs
     * This method is meant to transform the query parameters into a more human
     * readable form. It is only executed when SEF mode is switched on.
     *
     * @param   array  &$query  An array of URL arguments
     *
     * @return  array  The URL arguments to use to assemble the subsequent URL.
     *
     * @since   3.3
     */
    public function build(&$query)
    {
        $segments = array();

        if (isset($query['view'])) {
            unset($query['view']);
        } 

        if(isset($query['id']) && isset($query['name']))                               
        {
            $segments[] = $query['id']."-".$query['name'];
            unset($query['id']);
            unset($query['name']);  
        }
        else if(isset($query['id']))                               
        {
            $segments[] = $query['id'];
            unset($query['id']);
        }
        return $segments;
    }

    /**
     * Parse method for URLs
     * This method is meant to transform the human readable URL back into
     * query parameters. It is only executed when SEF mode is switched on.
     *
     * @param   array  &$segments  The segments of the URL to parse.
     *
     * @return  array  The URL attributes to be used by the application.
     *
     * @since   3.3
     */
    public function parse(&$segments)
    {
        $vars = array();
        $vars['view'] = "name-of-ONE-of-the-view";    
        $aux = explode(":",$segments[0]);     
        $vars['id'] = $aux[0];    
        return $vars;
    }
}

P.S. Since the name of the views are the same as the parameters I use to change the module layouts, I tried to access the params and then echo the current one on $vars['view'] but it did not work, unfortunately.

  • Please provide a var_dump of your $segments array. – Dennis Heiden Mar 1 '18 at 10:37
  • And please take a look into docs.joomla.org/J3.x:Supporting_SEF_URLs_in_your_component / in Joomla 3.8 (should update to that version) we have a new router class. – Dennis Heiden Mar 1 '18 at 10:41
  • Hi Dennis, thank you very much for your reply. The var_dump of my $segments array is: array(1) { [0]=> string(X) "name-of-the-view/id-name" } . For example: array(1) { [0]=> string(30) "player/56-alexandor-vandemar" } . Unfortunately, I can't update Joomla! to the 3.8 version because of corporate's choices, but they are working on that. – OlafGizursson Mar 5 '18 at 11:42
  • Did you try the router from my answer? I really don't get, as far as I could see, you just need to use a standard router and valid views in your component. Sometimes it helps if you turn off SEF in Joomla config to see the parameter url for debugging. – Dennis Heiden Mar 5 '18 at 13:01
1

I did something like this on my older J3 components:

<?php

defined('_JEXEC') or die;

class MyComponentRouter extends JComponentRouterBase
{
    /**
     * Build method for URLs
     * This method is meant to transform the query parameters into a more human
     * readable form. It is only executed when SEF mode is switched on.
     *
     * @param   array  &$query  An array of URL arguments
     *
     * @return  array  The URL arguments to use to assemble the subsequent URL.
     *
     * @since   3.3
     */
    public function build(&$query)
    {
        $segments = array();
        $view     = null;

        if (isset($query['task']))
        {
            $taskParts  = explode('.', $query['task']);
            $segments[] = implode('/', $taskParts);
            $view       = $taskParts[0];
            unset($query['task']);
        }

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

        if (isset($query['id']))
        {
            if ($view !== null)
            {
                $segments[] = $query['id'];
            }
            else
            {
                $segments[] = $query['id'];
            }

            unset($query['id']);
        }

        return $segments;
    }

    /**
     * Parse method for URLs
     * This method is meant to transform the human readable URL back into
     * query parameters. It is only executed when SEF mode is switched on.
     *
     * @param   array  &$segments  The segments of the URL to parse.
     *
     * @return  array  The URL attributes to be used by the application.
     *
     * @since   3.3
     */
    public function parse(&$segments)
    {
        $vars = array();

        // View is always the first element of the array
        $vars['view'] = array_shift($segments);

        while (!empty($segments))
        {
            $segment = array_pop($segments);

            // If it's the ID, let's put on the request
            if (is_numeric($segment))
            {
                $vars['id'] = $segment;
            }
            else
            {
                $vars['task'] = $vars['view'] . '.' . $segment;
            }
        }

        return $vars;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.