Silverstripe:清理URL

时间:2016-09-20 00:51:35

标签: php content-management-system silverstripe

我目前有一个显示员工档案的模块。它的工作原理如下:

  • 员工持有人:显示所有员工的列表
  • 员工档案:显示来自" StaffMember"的数据库记录。表

员工档案使用模板" StaffHolder_profile.ss"。显然,' profile'是显示员工档案的操作。显示配置文件操作适用于如下所示的URL:

' http:// / staff / profile / 记录ID &# 39;

我被要求删除"个人资料/ ID"来自这些网址。据我所知,这是不可能的,因为模块依赖于URL才能工作? (它使用URL变量...)

这是真的吗?有没有办法可以实现"清理"该网址,以便新网址为" http://domain/staff/staff-member-name"

2 个答案:

答案 0 :(得分:2)

您可以通过覆盖页面控制器索引中的路由参数“Action”来实现该特定的url模式。我不建议这样做。相反,我建议你为页面创建一个特定的Action,例如'domain / staff / view / ...'。我的下面示例会覆盖路由参数'Action',但仅用于解决您的问题。

您可以将您的标识符作为名称的基础,但缺少详细信息和/或匹配名称等不一致会产生问题 - 这些示例中未涵盖其中的大部分内容。唯一标识符会好得多。

我没有测试运行任何此代码,所以抱歉错误。

-

示例1:较慢,但需要的工作量较少。

StaffHolder_Controller:

public function index() {

    /**
     * @internal This will display the first match ONLY. If you'd like to
     * account for member's with exactly the same name, generate and store the
     * slug against their profile... See Example 2 for that.
     */

    // Re-purpose the 'Action' URL param (not advisable)
    $slug = $this->getRequest()->param('Action');

    // Partial match members by first name
    $names = explode('-', $slug);
    $matches = Member::get()->filter('FirstName:PartialMatch', $names[0]);

    // Match dynamically
    $member = null;
    foreach($matches as $testMember) {
        // Uses preg_replace to remove all non-alpha characters
        $testSlug = strtolower(
            sprintf(
                '%s-%s',
                preg_replace("/[^A-Za-z]/", '', $testMember->FirstName),
                preg_replace("/[^A-Za-z]/", '', $testMember->Surname)
            )
        ); // Or use Member::genereateSlug() from forthcoming example MemberExtension

        // Match member (will stop at first match)
        if($testSlug == $slug) {
            $member = $testMember;
            break;
        }
    }

    // Handle invalid requests
    if(!$member) {
        return $this->httpError(404, 'Not Found');
    }

    /**
     * @internal If you're lazy and want to use your existing template
     */
    return $this->customise(array(
        'Profile' => $member
        ))->renderWith(array('StaffHolder_profile', 'Page'));

}

-

示例2:

config.yml:

Member:
  extensions:
    - MemberExtension

MemberExtension.php:

class MemberExtension extends DataExtension {

    private static $db = array(
        'Slug' => 'Varchar' // Use 'Text' if it's likely that there will be a value longer than 255
    );

    public function generateSlug() {
        // Uses preg_replace to remove all non-alpha characters
        return strtolower(
            sprintf(
                '%s-%s',
                preg_replace("/[^A-Za-z]/", '', $this->owner->FirstName),
                preg_replace("/[^A-Za-z]/", '', $this->owner->Surname)
            )
        );
    }

    public function onBeforeWrite() {

        // Define slug
        if(!$this->owner->Slug)) {
            $slug = $this->generateSlug();

            $count = Member::get()->filter('Slug:PartialMatch', $slug)->Count();

            // Check for unique
            $unique = null;
            $fullSlug = $slug;
            while(!$unique) {
                // Add count e.g firstname-surname-2
                if($count > 0) {
                    $fullSlug = sprintf('%s-%s', $slug, ($count+1));
                }

                // Check for pre-existing
                if(Member::get()->filter('Slug:PartialMatch', $fullSlug)->First()) {
                    $count++; // (Try again with) increment
                } else {
                    $unique = true;
                }
            }

            // Update member
            $this->owner->Slug = $fullSlug;
        }

    }

}

StaffHolder_Controller:

public function index() {

    // Re-purpose the action URL param (not advisable)
    $slug = $this->getRequest()->param('Action');

    // Check for member
    $member = Member::get()->filter('Slug', $slug)->first();

    // Handle invalid requests
    if(!$member) {
        return $this->httpError(404, 'Not Found');
    }

    /**
     * @internal If you're lazy and want to use your existing template
     */
    return $this->customise(array(
        'StaffMember' => $member
        ))->renderWith('StaffHolder_profile');

}

答案 1 :(得分:0)

嵌套网址是SiteTree类的默认行为。每个子页面URL段都会添加到父完整URL中。因此,使用以下页面层次结构,您将获得干净的URL

Staff (StaffHolder, /staff)
|- John Doe (StaffProfilePage, /staff/john-doe)
|- Marie Smith (StaffProfilePage, /staff/marie-smith)

您可以使用

在StaffHolder.ss模板中获取人员资料页面列表
<ul>
<% loop $Children %>
    <li><a href="$Link">$Title</a></li>
<% end_loop %>
</ul>