【问题标题】:Silverstripe Controller docs confusingSilverstripe 控制器文档令人困惑
【发布时间】:2016-11-24 01:15:25
【问题描述】:

根据文档,“我们需要定义可以访问此控制器的 URL。在我们的例子中,TeamsController 应该在 http://yoursite.com/teams/ 可见,玩家自定义操作在 http://yoursite.com/team/players/。”。但是控制器被定义为

<?php

    class TeamController extends Controller {

    private static $allowed_actions = array(
        'players',
        'index'
    );

    public function index(HTTPRequest $request) {
        // ..
    }

    public function players(HTTPRequest $request) {
        print_r($request->allParams());
    }
}

?>

配置:

Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'teams//$Action/$ID/$Name': 'TeamController'

这是正确的吗?

【问题讨论】:

  • 请不要使用关闭?&gt;标签,它不是必需的,如果你不小心在它后面有一个空格或其他东西,它可能会破坏某些东西(例如自定义http标头),因为它会被输出直接。

标签: php content-management-system silverstripe


【解决方案1】:

TLDR;

是的,理论上是正确的。除了一个小错别字。

更长的答案

您想在访问 url http://yoursite.com/team/players/ 时查看玩家列表。这个 URL 有四个部分:

  1. 协议 http://
  2. yoursite.com
  3. 域之后的第一部分,/team
  4. 域之后的第二部分,/player

协议和域由您的 SilverStripe 安装的网络服务器解析。现在来了 /team。这应该映射到您的 TeamController 类。因此我们需要定义一个 route 以便 SilverStripe 知道,所有以 team 开头的东西都应该由这个控制器处理。我们在 yml.config 中定义路由,我更喜欢单独的路由文件,例如*/mysite/_config/routes.yml':

Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'team//$Action/$ID/$Name': 'TeamController'

因此,任何以“团队”开头的请求(在域之后)(请注意,在您的示例中,您有 团队,这是一个破坏一切的重要错字)被路由到 TeamController类和第二个参数(在我们的示例中,“players”作为$Action 参数传递。TeamController 本身不知道第一部分,它只是获取您在路由中定义的其他参数。

这是由$allowed_actions 在您的TeamControllerclass 中映射的:

private static $allowed_actions = array(
    'players',
    'index'
);

因此直接映射到呈现输出的players 方法。

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2017-05-30
    相关资源
    最近更新 更多