Salesforce 站点

Salesforce Sites 允许您通过继承来构建自定义页面和 Web 应用程序 闪电平台功能,包括分析、工作流程和审批,以及可编程 逻辑。

您可以使用 和 类的方法在 Apex 中管理您的 Salesforce 站点。SiteCookie

  • 重写 Salesforce 站点的 URL 站点提供内置逻辑,可帮助您显示用户友好的 URL 和指向站点
    访问者的链接。创建规则以重写在地址栏中键入的、从书签启动的或从外部网站链接的 URL 请求。您还可以创建规则来重写网站页面中链接的 URL。URL 重写不仅使 URL 对用户更具描述性和直观性,还允许搜索引擎更好地索引您的网站页面。

重写 Salesforce 站点的 URL

协作平台提供了内置逻辑,可帮助您显示用户友好的网址和指向 网站访问者。

创建规则以重写键入的 URL 请求 进入地址栏,从书签启动,或从外部网站链接。您可以 此外,还要创建规则来重写网站页面中链接的 URL。URL 重写不 只会使 URL 对用户更具描述性和直观性,它允许搜索引擎 更好地为您的网站页面编制索引。

例如,假设您有一个博客网站。如果没有 URL 重写,博客条目的 URL 可能如下所示: https://myblog.my.salesforce-sites.com/posts?id=003D000000Q0PcN

通过 URL 重写,您的用户可以按日期和标题访问博客文章,而不是 按记录 ID。您的新年前夜帖子的网址可能是:https://myblog.my.salesforce-sites.com/posts/2019/12/31/auld-lang-syne

您还可以重写网站页面中显示的链接的 URL。如果你的新年前夜帖子 包含指向您的情人节帖子的链接,链接网址可能会显示:https://myblog.my.salesforce-sites.com/posts/2019/02/14/last-minute-roses

若要重写站点的 URL,请创建一个 Apex 类,该类将原始 URL 映射到 用户友好的 URL,然后将 Apex 类添加到您的网站。

若要了解 中的方法,请参阅 UrlRewriter 接口。Site.UrlRewriter interface

创建 Apex 类

您创建的 Apex 类必须实现提供的接口。通常,它必须具有 以后 形式:

Site.UrlRewriter

global class yourClass implements Site.UrlRewriter {
    global PageReference mapRequestUrl(PageReference
            yourFriendlyUrl)
    global PageReference[] generateUrlFor(PageReference[]
            yourSalesforceUrls);
}

在创建 Apex 类时,请考虑以下限制和建议:类和方法必须是全局的Apex 类和方法必须全部为 。global类必须同时包含这两种方法Apex 类必须同时实现 和 方法。如果您不想使用以下之一 方法,只需让它返回即可。mapRequestUrlgenerateUrlFornull重写仅适用于 Visualforce 网站页面传入的 URL 请求只能映射到关联的 Visualforce 页面 与您的网站。无法映射到标准页面、图像或其他 实体。要重写网站网页上链接的网址,请使用带有 merge 变量的函数。例如 以下链接到名为 我的页面:!URLFOR$Page

<apex:outputLink value="{!URLFOR($Page.myPage)}"></apex:outputLink>

注意

Visualforce 元素 不受 的影响。<apex:form>forceSSL=”true”urlRewriter请参阅 Visualforce 开发人员的 指南。编码的 URL使用该接口获取的 URL 已编码。如果需要 访问 URL 的未编码值,请使用 EncodingUtil 类的方法。Site.urlRewriterurlDecode受限制的字符用户友好的 URL 必须与 Salesforce URL 不同。带有 3 个字符的实体前缀或 15 或 18 个字符的 ID 不是 重写。您不能在用户友好网址或重写的网址中使用句点,但以下情况除外 对于路径 组件,不能在 URL 末尾使用。.well-known受限制的字符串不能使用以下保留字符串作为第一个路径组件 在用户友好的 URL 或重写的站点基本 URL 之后 网址。网站基本 URL 之后的第一个过去组件的一些示例 是 baseURL https://.my.salesforce-sites.com/baseURL, https://.my.salesforce-sites.com/pathPrefix/baseURL, https://custom-domain/pathPrefix/baseURL,以及 https://.my.salesforce-sites.com/pathPrefix/baseURL/another/path。MyDomainNameMyDomainNameMyDomainName

  • apexcomponent
  • apexpages
  • aura
  • chatter
  • chatteranswers
  • chatterservice
  • cometd
  • ex
  • faces
  • flash
  • flex
  • google
  • home
  • id
  • ideas
  • idp
  • images
  • img
  • javascript
  • js
  • knowledge
  • lightning
  • login
  • m
  • mobile
  • ncsphoto
  • nui
  • push
  • resource
  • saml
  • sccommunities
  • search
  • secur
  • services
  • servlet
  • setup
  • sfc
  • sfdc
  • sfdc_ns
  • sfsites
  • site
  • style
  • vote
  • WEB-INF
  • widg

不能在重写的末尾使用以下保留字符串 URL 路径:

  • /光环
  • /auraFW
  • /auraResource
  • /AuraJLoggingRPCService
  • /AuraJLVRPC服务
  • /AuraJRPC服务
  • /dbcthumbnail
  • /HelpAndTrainingDoor
  • /htmldbcthumbnail
  • /升
  • /米
  • /移动

仅相对路径PageReference.getUrl() 方法 仅返回紧跟在主机名后面的 URL 部分,或者 站点前缀(如果有)。例如,如果您的 URL 是 https://mycompany.my.salesforce-sites.com/sales/MyPage?id=12345, 其中“sales”是网站前缀,仅返回 /MyPage?id=12345。您无法重写域或网站前缀。仅限唯一路径您无法将 URL 映射到与您的网站同名的目录 前缀。例如,如果您的网站网址为 https://acme.my.salesforce-sites.com/help, 其中“help”是站点前缀,则不能指向 URL 到帮助/页面。生成的路径 https://acme.my.salesforce-sites.com/help/help/page、 将作为 https://acme.my.salesforce-sites.com/help/page 返回。批量查询为了提高页面生成性能,请批量执行任务 而不是一次一个的方法。generateUrlFor强制执行字段唯一性确保您选择用于重写 URL 的字段是唯一的。用 SOQL 中用于查询的唯一字段或索引字段可能会得到改进 性能。

向站点添加 URL 重写

创建 URL 重写 Apex 类后,请按照以下步骤将其添加到 您的网站:

  1. 在“设置”中,输入“快速” “查找”框,然后选择“站点”。Sites
  2. 单击“新建”或单击“编辑” 现有站点。
  3. 在“站点编辑”页上,为“URL 重写器”选择一个 Apex 类 类。
  4. 点击保存

注意

如果您的网站上启用了 URL 重写,则所有 PageReferences 都是 通过 URL 重写器传递。设置为 且不是 0 的 PageReferences 返回重定向的 URL,而不是 重写的 URL。redirecttrueredirectCode

代码示例

在此示例中,我们有一个由两个 Visualforce 页面组成的简单站点:mycontact 和 myaccount。确保您为两者启用了“读取”权限 在尝试示例之前。每个页面都使用标准控制器作为其对象 类型。联系页面包括指向父帐户的链接以及联系人 详。

在实现重写之前,地址栏和链接 URL 显示记录 ID(a 随机 15 位字符串),如“之前”图所示。启用重写后, 地址栏和链接显示更多用户友好的重写 URL,如“之后”所示 图。

用于重写这些页面的 URL 的 Apex 类显示在 URL 重写 Apex 类示例中,并附有详细的注释。

示例网站页面

本部分显示此部分中使用的客户和联系人页面的 Visualforce 例。帐户页面对帐户使用标准控制器,只不过是一个 标准详情页面。此页面应命名为 我的帐户。

<apex:page standardController="Account">
    <apex:detail relatedList="false"/>
</apex:page>

联系人页面使用标准控制器进行联系人,由两部分组成。 第一部分使用函数和合并变量链接到父帐户;第二个只是提供联系方式。请注意, Visualforce 页面不包含任何重写逻辑,但 .此页面应命名为 mycontact。

URLFOR$PageURLFOR

<apex:page standardController="contact">
    <apex:pageBlock title="Parent Account">
        <apex:outputLink value="{!URLFOR($Page.mycontact,null, 
                [id=contact.account.id])}">{!contact.account.name}
                </apex:outputLink>
    </apex:pageBlock>
    <apex:detail relatedList="false"/>
</apex:page>

示例 URL 重写 Apex 类

用作站点的 URL 重写器的 Apex 类使用该方法来映射传入的 URL 请求到正确的 Salesforce 记录。它还使用该方法重写 链接到帐户页面更加人性化 形式。

mapRequestUrlgenerateUrlFor

global with sharing class myRewriter implements Site.UrlRewriter {

    //Variables to represent the user-friendly URLs for
    //account and contact pages
    String ACCOUNT_PAGE = '/myaccount/';
    String CONTACT_PAGE = '/mycontact/';
    //Variables to represent my custom Visualforce pages
    //that display account and contact information
    String ACCOUNT_VISUALFORCE_PAGE = '/myaccount?id=';
    String CONTACT_VISUALFORCE_PAGE = '/mycontact?id=';

    global PageReference mapRequestUrl(PageReference
            myFriendlyUrl){
        String url = myFriendlyUrl.getUrl();

        if(url.startsWith(CONTACT_PAGE)){
            //Extract the name of the contact from the URL
            //For example: /mycontact/Ryan returns Ryan
            String name = url.substring(CONTACT_PAGE.length(),
                    url.length());

            //Select the ID of the contact that matches
            //the name from the URL
            Contact con = [SELECT Id FROM Contact WHERE Name =:
                    name LIMIT 1];

            //Construct a new page reference in the form
            //of my Visualforce page
            return new PageReference(CONTACT_VISUALFORCE_PAGE + con.id);
        }
        if(url.startsWith(ACCOUNT_PAGE)){
            //Extract the name of the account
            String name = url.substring(ACCOUNT_PAGE.length(),
                    url.length());

            //Query for the ID of an account with this name
            Account acc = [SELECT Id FROM Account WHERE Name =:name LIMIT 1];

           //Return a page in Visualforce format
            return new PageReference(ACCOUNT_VISUALFORCE_PAGE + acc.id);
        }
        //If the URL isn't in the form of a contact or
        //account page, continue with the request
        return null;
    }
    global List<PageReference> generateUrlFor(List<PageReference> 
            mySalesforceUrls){
        //A list of pages to return after all the links 
        //have been evaluated
        List<PageReference> myFriendlyUrls = new List<PageReference>();
        
        //a list of all the ids in the urls
        List<id> accIds = new List<id>();
        
        // loop through all the urls once, finding all the valid ids
        for(PageReference mySalesforceUrl : mySalesforceUrls){
        //Get the URL of the page
        String url = mySalesforceUrl.getUrl();
 
            //If this looks like an account page, transform it
            if(url.startsWith(ACCOUNT_VISUALFORCE_PAGE)){
                //Extract the ID from the query parameter
                //and store in a list
                //for querying later in bulk.
                        String id= url.substring(ACCOUNT_VISUALFORCE_PAGE.length(),
                        url.length());
                        accIds.add(id);
            }
        }

    // Get all the account names in bulk
    List <account> accounts = [SELECT Name FROM Account WHERE Id IN :accIds];

    // make the new urls
    Integer counter = 0;

    // it is important to go through all the urls again, so that the order
    // of the urls in the list is maintained. 
    for(PageReference mySalesforceUrl : mySalesforceUrls) {

       //Get the URL of the page
       String url = mySalesforceUrl.getUrl();

       if(url.startsWith(ACCOUNT_VISUALFORCE_PAGE)){
         myFriendlyUrls.add(new PageReference(ACCOUNT_PAGE + accounts.get(counter).name));
         counter++;
       } else {
         //If this doesn't start like an account page,
         //don't do any transformations
         myFriendlyUrls.add(mySalesforceUrl);
       }
    }
   
    //Return the full list of pages
    return myFriendlyUrls;
  }

}

重写之前和之后

下面是实现 Apex 类以重写 原始站点 URL。请注意第一张图中基于 ID 的 URL,以及 第二个用户友好的 URL。

重写之前的网站 URL之前的网站网址此图中编号的元素是:

  1. 重写之前联系人页面的原始 URL
  2. 从联系页面到父帐户页面的链接
  3. 重写前指向帐户页面的链接的原始 URL,如 浏览器的状态栏

重写后的网站 URL之后的网站网址此图中编号的元素是:

  1. 重写后联系人页面的重写 URL
  2. 从联系页面到父帐户页面的链接
  3. 重写后指向帐户页面的链接的重写 URL,如 浏览器的状态栏