0

How can I get & set the fragment(# parameter in URL) in the URL with the Joomla's existing build & parse method in the router file of the custom component?

3
  • You have a Joomla Stack Exchange account; would you like Stack Overflow moderators to migrate this question to the purpose-built community? Commented Oct 17, 2020 at 1:35
  • Yes, please. I need the solution/suggestion ASAP Commented Oct 17, 2020 at 16:43
  • Can you please edit your question to include what you have tried so far and any relevant links that you found while researching? Please read How to Ask and take our tour. Commented Oct 18, 2020 at 8:24

1 Answer 1

1

Your question is not fully clear about using Fragments in Joomla Routers and it is because I do not really see what you really want to do with the fragments in your component's router. Few things I’d like to clear about that though.

#Fragments are not handled by the Joomla Router class basically since #fragments do not require routing. It is handled by the Joomla Uri class however as a URI component (out of other defined uri components). But since uri #fragments - otherwise pointers to "html anchors" - show direction to an HTML element and the browser will scroll that element into view, thus you do not have to write a separate router for them in your Joomla component. It is processed without any router work on the page where your router sent you.

The fragment identifier functions differently to the rest of the URI: its processing is exclusively client-sided with no participation from the web server,...(from wikipedia)

In Joomla however you can set and get and “manipulate” the fragment part of the URL by the Joomla Uri class. This is the class you can build, parse or manipulate URLs with in Joomla.

So if you create an Html anchor element on any page in Joomla:

<a name=“my_anchor”>#This is my Fragment here</a>

Then you can point to that HTML anchor by adding that anchor name (or id) as the fragment to the end of the URL of the page where that anchor is and you can of course use the Joomla Uri class for doing that. Thus in your PHP code where you handle URLs (and/or routing maybe) you can get and set and might "manipulate" that URL part. As an example, on the top of your PHP file include Joomla Uri class (you can try this code in any layout file):

<?php

use \Joomla\CMS\Uri\Uri;

// Returns the global JUri object, only creating it if it doesn't already exist.    
$uriInst = Uri::getInstance(); 

// adding a fragment to the url object you got    
$uriInst->setFragment('my_anchor');

echo "<br>this is the Uri instance's string url with the added fragment: " . $uriInst; // or
echo "<br>Uri instance plus fragment to string:" . $uriInst->toString() . "<br>";

// to get the fragment part from the Uri object    
$myFragm = $uriInst->getFragment(); 

echo "<br>" . $myFragm;

?>

Now, if you dump the Uri object at this point: var_dump($uriInst);, then you will see something like this:

object(Joomla\CMS\Uri\Uri)[345]
  protected 'uri' => string 'http://mysite/index.php?option=com_mycomponent&layout=default_page_html' (length=76)
  protected 'scheme' => string 'http' (length=4)
  protected 'host' => string 'mysite' (length=7)
  protected 'port' => null
  protected 'user' => null
  protected 'pass' => null
  protected 'path' => string '/index.php' (length=10)
  protected 'query' => string 'option=com_mycomponent&layout=default_page_html' (length=51)
  protected 'fragment' => string 'my_anchor' (length=10)
  protected 'vars' => 
    array (size=2)
      'option' => string 'com_component' (length=19)
      'layout' => string 'default_page_html' (length=17)

You see, a perfect URI object with all of the uri components needed for the Router builder if you would like to use that there too. But it is important to note that the router builder/parser is expected to work with the ’query’ part of the URI/URL. I hope that the above helps a bit to clear the subject more for you and you will be able to use it.

references: URI Fragments , URLs in Joomla , Joomla Uri class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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