0

I am creating new Component in salesforce lightning.

I want to get current page complete url in server controller, Can you please help me to get url.

I have tried ApexPages.currentPage().getUrl() method this will print current url in apex controller while did't print any thing in component controller. Here is my server controller approach to get URL:

public with sharing class OpportunityController {
    ......
    @AuraEnabled
    public static List<String> geturl() {
        String[] listing = new List<String>{};
        // String url =  'testing';
        String url =  ApexPages.currentPage().getUrl();
        listing.add(url);
        return listing;
    } 
}
3

The apex controller associated with the lightning component is not tightly coupled as you see in case of visualforce.

The methods you use from apex classes are static methods and are stateless. The ApexPages.currentPage() will return null since the class is not associated with any visualforce page. To put the whole story short, you can not directly access page URL in Apex class in case of lightning components.

Alternatively, you can access the URL using lightning component and then pass it to Apex class invoking an action.

Here is how you can do that:

Fetch the page URL like this in lightning component controller:

var sPageURL = decodeURIComponent(window.location.search.substring(1));

You will have the whole URL in sPageURL variable.

You can pas it to your apex class method using an action:

var action = component.get('c.doSomethingWithPageURL');/aasuming you need to pass the URL to this doSomethingWithPageURL function.
action.setParams({
pageURL : sPageURL
});
action.setCallback(this, function(response){...});
$A.enqueueAction(action);
0

It should work. I have done the same and I could see the page name in debug logs.

Page:

<apex:page controller="OppTestController" action="{!getmethod1}">

</apex:page>

Controller:

public with sharing class OppTestController {

@auraEnabled
public static void getmethod1(){
    String url =  ApexPages.currentPage().getUrl();
    system.debug('+++URL'+url);
}
}

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.