【问题标题】:How to Properly Setup Silvertripe Routes and Controllers and Templates如何正确设置 Silvertripe 路由和控制器和模板
【发布时间】:2016-03-18 02:08:20
【问题描述】:

如何正确设置 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'

输入时我的网址是http://127.0.0.1/silverstripe/test/

但是来自测试控制器的 print_r 没有出现也返回 $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.

【问题讨论】:

    标签: php yaml silverstripe


    【解决方案1】:

    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 查询参数调试您的请求。

    【讨论】:

    • 您的意思是 TestController 而不是 TextController?
    • 是的。编辑了答案
    • 测试方法处理 /test/ 而不是 /test/test?
    • 否,测试方法处理动作“测试”并对应于 /test/test 请求。例如,您有一个控制器Library_Controller 处理/library url 段,然后Library_Controller::books() 方法处理/library/books url。
    • $this->renderWith(['TemplateName','Controller']) 有效,但 {$DefaultTemplate}_{$Action}.ss) 无效?
    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多