liferayliferay-6friendly-url

Liferay friendly url with optional param


My URL have some params, so i use friendly URLs to solve that ugly URL liferays generates by default.

couple of this params sometimes are empty, that mean its no needed, but other times give me some ID i use for a query.

Example param not null:

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-7
//it works

Example null param:

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-null
//it works

With null param it works, but i dont like to see a null on my URL.

I want something like "":

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-
//Doesnt work

But it doesnt work, its like the URL doesnt match the friendly URL pattern when a param its empty.

   <route>
       <pattern>/search/idTable-{idTable}</pattern>
       <generated-parameter name="idTable">{idTable}</generated-parameter>
       <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
       <!--more implicit params-->
   </route>

How can i specify optionality of a parameter?


Solution

  • Its like all vars of friendly url use a regex. You can change/override this regex like this: {idTable:\d}

    <route>
        <pattern>/search/idTable-{idTable:.*}</pattern>
        <generated-parameter name="idTable">{idTable}</generated-parameter>
        <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
        <!--more implicit params-->
    </route>
    

    i used .* as regex for 0 or more characters, but i dont know if it will bring me problems later. If anyone knows why is not a good idea to use that regex comment it please.

    Upgraded the regex with \d* to search only digits or empty: {idTable:\d*}

    Info: Making URLs friendlier 7.0