php 银色条纹:正在清理URL

6qftjkof  于 2023-03-16  发布在  PHP
关注(0)|答案(2)|浏览(93)

我目前有一个模块,显示一个员工的个人资料。它的工作原理如下:

  • 工作人员保持器:显示所有职员的列表
  • 员工简介:显示“StaffMember”表中的数据库记录。

员工配置文件使用模板“StaffHolder_profile.ss”。显然,“profile”是显示员工配置文件的操作。显示配置文件操作适用于如下所示的URL:
'http:///职员/配置文件/记录的ID'
我被要求从这些URL中删除“profile/id”。据我所知,这是不可能的,因为模块依赖于URL才能工作?(它使用URL变量...)
这是真的吗?有没有一种方法可以从本质上“清理”URL,使新的URL将是“http://domain/staff/staff-member-name

uujelgoq

uujelgoq1#

您可以通过覆盖页面控制器索引中的路由参数“Action”来实现特定的URL模式。我不建议这样做。相反,我建议您为页面创建一个特定的Action,例如“domain/staff/view/...”。我下面的示例确实覆盖了路由参数“Action”,但只是为了满足您的问题。
您可以基于名称创建标识符,但缺少详细信息和/或匹配名称等不一致会产生问题-这些示例中没有涵盖其中的许多问题。唯一标识符会好得多。
我还没有测试运行任何这段代码,所以很抱歉的错误。

**示例1:**速度较慢,但需要的工作较少。

任职人员_管理员:

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;
        }

    }

}

任职人员_管理员:

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');

}
wwwo4jvm

wwwo4jvm2#

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

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

您可以使用以下命令在www.example.com模板中获取员工概况页面列表StaffHolder.ss

<ul>
<% loop $Children %>
    <li><a href="$Link">$Title</a></li>
<% end_loop %>
</ul>

相关问题