如何正确设置 Silvertripe 路由、控制器和模板

How to Properly Setup Silvertripe Routes and Controllers and Templates

如何正确设置 Silvertripe 路由、控制器和页面?我遵循了如下所示的开发人员文档,

TestController.php

    class TestController extends Controller {

        private static $allowed_actions = array(
            'test'
        );

        public function test(SS_HTTPRequest $request) {
            print_r('Executing Test Controller inside TestController');
        }

    }

routes.yml

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'test/': 'TestController'

我输入的url是http://127.0.0.1/silverstripe/test/

但是来自测试控制器的 print_r 也没有出现 return $this->renderWith("Test") 没有工作。有一个实际的 Test.ss.

这就是输出结果

入门

开始使用 SilverStripe 框架:

Create a Controller subclass (doc.silverstripe.org/framework/en/topics/controller)
Setup the routes.yml to your Controller (doc.silverstripe.org/framework/en/reference/director#routing).
Create a template for your Controller (doc.silverstripe.org/framework/en/reference/templates)

社区资源

silverstripe.org/forum Discussion forums for the development community.

silverstripe.org/irc IRC channel for realtime support and discussions.

doc.silverstripe.org Searchable developer documentation, how-tos, tutorials, and reference.

api.silverstripe.org API documentation for PHP classes, methods and properties.

Director.rules 配置 test: TestController 将 '/test/$Action' 模式的任何 URL 绑定到 TestController。默认操作称为 index.

因此,如果您只想处理一个动作,则不需要 $allowed_actions,并将 test 方法重命名为 index.

您当前的控制器 test 方法处理 /test/test 请求。

默认模板名称由具有模式 TemplateName_Controller 的控制器名称确定。下划线字符拆分名称,相当于调用$this->renderWith(['TemplateName','Controller']).

在您的情况下,默认控制器模板是 "TestController.ss",自定义操作模板可能是 "TestController_Test.ss"(如模式 {$DefaultTemplate}_{$Action}.ss

您可以使用 ?debug=1?debug_request=1 查询参数调试您的请求。