Today’s post we are discussing how to get the current URL in phtml file in Magento 2. As we know we have access to $block
object in the phtml, we can utilize that for this purpose.
Let’s print methods for the class $block->getRequest()
echo "<pre>";
print_r(get_class_methods($block->getRequest()));
It produces the output like this
Array
(
[0] => __construct
[1] => getOriginalPathInfo
[2] => getPathInfo
[3] => setPathInfo
[4] => isDirectAccessFrontendName
[5] => getBasePath
[6] => getFrontName
[7] => setRouteName
[8] => getRouteName
[9] => setControllerModule
[10] => getControllerModule
[11] => initForward
[12] => getBeforeForwardInfo
[13] => isAjax
[14] => getDistroBaseUrl
[15] => getDistroBaseUrlPath
[16] => getUrlNoScript
[17] => getFullActionName
[18] => __sleep
[19] => isSafeMethod
[20] => getModuleName
[21] => setModuleName
[22] => getControllerName
[23] => setControllerName
[24] => getActionName
[25] => setActionName
[26] => getRequestString
[27] => getAlias
[28] => setAlias
[29] => getParam
[30] => setParam
[31] => getParams
[32] => setParams
[33] => clearParams
[34] => getScheme
[35] => setDispatched
[36] => isDispatched
[37] => isSecure
[38] => getCookie
[39] => getServerValue
[40] => getQueryValue
[41] => setQueryValue
[42] => getPostValue
[43] => setPostValue
[44] => __get
[45] => get
[46] => __isset
[47] => has
[48] => getHeader
[49] => getHttpHost
[50] => getClientIp
[51] => getUserParams
[52] => getUserParam
[53] => setRequestUri
[54] => getBaseUrl
[55] => isForwarded
[56] => setForwarded
[57] => getContent
[58] => setCookies
[59] => getRequestUri
[60] => setBaseUrl
[61] => setBasePath
[62] => setServer
[63] => getServer
[64] => setEnv
[65] => getEnv
[66] => fromString
[67] => setMethod
[68] => getMethod
[69] => setUri
[70] => getUri
[71] => getUriString
[72] => setQuery
[73] => getQuery
[74] => setPost
[75] => getPost
[76] => setFiles
[77] => getFiles
[78] => getHeaders
[79] => isOptions
[80] => isPropFind
[81] => isGet
[82] => isHead
[83] => isPost
[84] => isPut
[85] => isDelete
[86] => isTrace
[87] => isConnect
[88] => isPatch
[89] => isXmlHttpRequest
[90] => isFlashRequest
[91] => renderRequestLine
[92] => toString
[93] => getAllowCustomMethods
[94] => setAllowCustomMethods
[95] => setVersion
[96] => getVersion
[97] => setHeaders
[98] => __toString
[99] => setMetadata
[100] => getMetadata
[101] => setContent
)
From this we can use the getRequestString()
method get the current url in Magento. Also, you can test the other methods as well, and see how this class working.
The final code will be like this
print $block->getRequest()->getRequestString()
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply