Posted By


d3developer on 08/08/10

Tagged


Statistics


Viewed 54 times
Favorited by 0 user(s)

@umityalcinalp URLRewriter


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. http://blog.sforce.com/sforce/2010/05/url-rewriting-for-your-customizing-your-site-urls.html
  2.  
  3.  
  4. global class ApexClassName implements Site.UrlRewriter {
  5.  
  6. global PageReference mapRequestUrl(PageReference externalUrl) {//}
  7.  
  8. global List<PageReference> generateUrlFor(List<PageReference> myForcedotcomUrls) {//...}
  9.  
  10. }
  11.  
  12. Note that a developer may not need all of the methods and may choose to return a null.
  13.  
  14. Here is the implementation of our the global Apex class that maps the URLs from our example:
  15.  
  16. global class myRewriter implements Site.UrlRewriter {
  17.  
  18. //Variables to represent the friendly URLs for pages
  19.  
  20. String DIRECTORY = '/friendly/';
  21.  
  22. //Variables to represent my custom Visualforce pages that display page information
  23.  
  24. String VISUALFORCE_PAGE = '/page?pageid=';
  25.  
  26. // The first global method for mapping external URL to an internal one
  27.  
  28. global PageReference mapRequestUrl(PageReference myFriendlyUrl){
  29.  
  30. String url = myFriendlyUrl.getUrl();
  31.  
  32. if(url.startsWith(DIRECTORY)){
  33.  
  34. String name = url.substring(DIRECTORY.length(),url.length());
  35.  
  36. //Select the ID of the page that matches the name from the URL
  37.  
  38. Page__c site_page = [select id from Page__c where name =:name LIMIT 1];
  39.  
  40. //Construct a new page reference in the form of my Visualforce page
  41.  
  42. return new PageReference(VISUALFORCE_PAGE + site_page.id);
  43.  
  44. }
  45.  
  46. //If the URL isn't in the form of a cmasforce page, continue with the request
  47.  
  48. return null;
  49.  
  50. }
  51.  
  52. // The second global method for mapping internal Ids to URLs
  53.  
  54. global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
  55.  
  56. //A list of pages to return after all the links have been evaluated
  57.  
  58. List<PageReference> myFriendlyUrls = new
  59.  
  60. List<PageReference>();
  61.  
  62. for(PageReference mySalesforceUrl : mySalesforceUrls){
  63.  
  64. //Get the URL of the page
  65.  
  66. String url = mySalesforceUrl.getUrl();
  67.  
  68. //If this looks like a page that needs to be mapped, transform it
  69.  
  70. if(url.startsWith(VISUALFORCE_PAGE)){
  71.  
  72. //Extract the ID from the query parameter
  73.  
  74. String id= url.substring(VISUALFORCE_PAGE.length(), url.length());
  75.  
  76. //Query for the name of the cmsforce page to put in the URL
  77.  
  78. Page__c site_page2 = [select name from Page__c where id =:id LIMIT 1];
  79.  
  80. //Construct the new URL
  81.  
  82. myFriendlyUrls.add(new PageReference(DIRECTORY+ site_page2.name));
  83.  
  84. }
  85.  
  86. else {
  87.  
  88. //If this doesn't start like an cmsforce page, don't do any transformations
  89.  
  90. myFriendlyUrls.add(mySalesforceUrl);
  91.  
  92. }
  93.  
  94. }
  95.  
  96. //Return the full list of pages
  97.  
  98. return myFriendlyUrls;
  99.  
  100. }
  101.  
  102. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.