Search code examples
phpjoomla

Joomla SEF URL to custom component task


I am quite new to Joomla! and building a custom component to provide API services. Below is the url to the component function :

http://www.example.com/index.php?option=com_componentA&task=ajax.functionA&format=json

I would like to change it to become :

http://www.example.com/componentA/functionA

Is there any idea I could achieve that?

I tried rewrite on .htaccess for that particular url but Joomla! will redirect it back to index.php.

Also tried installing joomsef by ARTIO but it did not work.

Thanks in Advance!


Solution

  • After a few more hours of testing I've solved it. I've added router.php to my component folder with two functions in it "[componentName]BuildRoute" and "[componentName]ParseRoute" to override original function. Refer Joomla! Documentation.

    [componentName]BuildRoute will be the function building SEF or custom URL.
    - This function will return an array where element in it will be concatenate to build the custom URL. - Depends on what you return in the array will determine the URLstructure.
    For eg. :

    http://www.example.com/index.php?option=com_componentA&task=ajax.functionA&format=json

    BuildRoute need to return array("ajax", "functionA");

    and translate into http://www.example.com/component/componentA/ajax/functionA

    [componentName]ParseRoute will be the function to convert your custom URL back to internal URL.

    • This function will input an array where each of the element will be use to determine the internal URL.

    • Then using the parameter to rebuild a return array that map back to internal link

    For Eg. :

    http://www.example.com/component/componentA/ajax/functionA

    ParseRoute parameter will be array("ajax", "functionA")

    ParseRoute needs to return array("task" => "ajax.createPolicyResult", "format"=>"json");

    http://www.example.com/index.php?option=com_componentA&task=ajax.functionA&format=json


    I cant remove the "component" from the URL else it won't work but its OK for me. As the "componentA" is needed as well so that it will call the router.php file in my custom component folder.

    Hope this help =)