silverstripe 如何在没有操作的情况下创建 holder/page
silverstripe how to create an holder/page without action
我试图在 silverstripe 中创建一个 holder/page,它只列出没有操作的 ID,就像博客页面一样,如果没有 ID (mydomain.com/programs/) 则显示列表所有程序如果有一个 ID (mydomain.com/programs/spinning-with-chris/) 比我想要一个程序的详细信息页面。
有没有人有如何执行此操作的示例?
假设您没有在控制器中定义自定义 URL 处理程序,默认情况下是这样的:
private static $url_handlers = array(
'$Action//$ID/$OtherID' => 'handleAction',
);
这意味着您的路线将是 routetocontroller/action
然后之后的任何内容都是可选的 (more info)。路由的第一部分将在您的 YAML 配置中定义,例如:
Director:
rules:
routetocontroller: YourControllerName
因此您的控制器需要公开索引操作:
private static $allowed_actions = array('index');
public function index(SS_HTTPRequest $request)
{
// Handle an ID passed
if ($id = $this->urlParams['ID']) {
return $this->doSomethingWithYourId($id);
}
// Otherwise, show all of your data
return $this->renderWith(array('YourTemplateName', array(
'YourDataList' => YourModel::get()
));
}
你可以在 YourTemplateName.ss
:
中循环显示
<% loop $YourDataList %>
<li><a href="$Link">$Title</a></li>
<% end_loop %>
这些只是起点示例,您当然需要根据自己的需要进行调整。
我试图在 silverstripe 中创建一个 holder/page,它只列出没有操作的 ID,就像博客页面一样,如果没有 ID (mydomain.com/programs/) 则显示列表所有程序如果有一个 ID (mydomain.com/programs/spinning-with-chris/) 比我想要一个程序的详细信息页面。
有没有人有如何执行此操作的示例?
假设您没有在控制器中定义自定义 URL 处理程序,默认情况下是这样的:
private static $url_handlers = array(
'$Action//$ID/$OtherID' => 'handleAction',
);
这意味着您的路线将是 routetocontroller/action
然后之后的任何内容都是可选的 (more info)。路由的第一部分将在您的 YAML 配置中定义,例如:
Director:
rules:
routetocontroller: YourControllerName
因此您的控制器需要公开索引操作:
private static $allowed_actions = array('index');
public function index(SS_HTTPRequest $request)
{
// Handle an ID passed
if ($id = $this->urlParams['ID']) {
return $this->doSomethingWithYourId($id);
}
// Otherwise, show all of your data
return $this->renderWith(array('YourTemplateName', array(
'YourDataList' => YourModel::get()
));
}
你可以在 YourTemplateName.ss
:
<% loop $YourDataList %>
<li><a href="$Link">$Title</a></li>
<% end_loop %>
这些只是起点示例,您当然需要根据自己的需要进行调整。