function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BrettEldridgeBrettEldridge 

Getting the Visual Force Page Name from a controller

Hi,

 

Very new here so sorry if my question is very obvious.

 

I am creating a website. Many pages, and the pages look different (Except for header and footer). But the Custom object I have created to store the text for each page is the same for all pages.

 

I have created 2 pages in visual force, very simple, they just display a field from the database depending on the page.

 

I have also created 2 controllers, they are identical, except for the database query, it says Where Name = 'X'   instead of Where Name = 'Y'

 

How can I use the same controller for these two pages?

 

I did get it to work by passing a name in the URL, eg..  /home?page=home

 

But why would I want the page on the URL twice?  Surly there is a way for the controller to read the first    /home   And then I can just use the page name to pull up a record. I will save each record in my cms object with name I used for the visualforce page.

 

I hope this makes sense, and hope it is possible.

 

Brett

 

LakshmanLakshman

Hi Bret,

 

Get the url in the constructor, validate it and do the required query.

If you are using app in SF then to get url you will have to do as below:

String strUrl = ApexPages.currentPage().getUrl(); //this method returns  /apex/PageA?id=001457932524698

No check the name of page as

if(strUrl.contains(PageA))

{

//Execute your query,Where Name = 'X' 

}else if(strUrl.contains(PageB))

{

//Execute your query,Where Name = 'Y' 

}

 

If you are using Site functionality, then you can get the URL as Site.getCurrentSiteUrl(); which returns http://force2b.test.cs3.force.com/PageA/ and then do the validations as above.

 

Let me know if this works for you.

 

Thanks,

Lakshman

BrettEldridgeBrettEldridge

Thanks. That makes sense to me.

 

The only thing is, If I didn't want to hard code A or B, and just wanted to be able to add pages, i would need to probably take the url and cut it into pieces.

 

Thanks again, and you answerd my question.

 

Brett