您可以通過覆蓋頁面控制器的索引中的路由參數'Action'來實現該特定的url模式。我不會建議這個。相反,我建議您爲頁面創建一個特定的操作,例如'domain/staff/view/...'。我下面的例子確實覆蓋了路由參數'Action',但只是爲了實現你的問題。
。
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');
}
你可以用自定義路由實現這一目標,但據推測會員名稱不是唯一的,所以你會遇到同名會員的問題。 –
您是否將其作爲前端頁面或CMS處理?你有用戶的登錄系統嗎? (然後可以在會話中找到當前用戶,請參閱CMS成員類)您是否使用SiteTree :: nested_urls? (然後StaffProfile可以是嵌套頁面)。是的,你可以在'StaffHolder_Controller :: index($ request)''中處理乾淨的URL,其中'$ name = $ request-> getParam('ID');' –
@GregSmirnov謝謝爲答覆。這是前端。是的,有一個登錄系統,但工作人員頁面將顯示一個數據對象列表作爲頁面。這不是當前用戶的個人資料。什麼是嵌套網址?那個控制器功能到底做了什麼? – Dallby