douzhenyu6533 2016-11-24 01:15
浏览 17
已采纳

Silverstripe Controller文档令人困惑

According to docu, "We need to define the URL that this controller can be accessed on. In our case, the TeamsController should be visible at http://yoursite.com/teams/ and the players custom action is at http://yoursite.com/team/players/.". But the controller is defined as

<?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());
    }
}

?>

Configuration:

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

Is this correct?

  • 写回答

1条回答 默认 最新

  • dongtao9095 2016-11-24 07:48
    关注

    TLDR;

    Yes, in theory it is correct. Beside a small typo.

    Longer answer

    You want to see the list of players when you access the url http://yoursite.com/team/players/. This URL has four parts:

    1. the protocol http://
    2. the domain yoursite.com
    3. the first part after the domain, /team
    4. the second part after the domain, /player

    Protocol and domain are resolved by your webserver with your SilverStripe installation. Now comes /team. This should map to your TeamController class. Therefor we need to define a route so SilverStripe knows, everything starting with team should be handled by this controller. We define routes in the yml.config, I prefer a seperate file for routes, e.g. */mysite/_config/routes.yml':

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

    So any request (after the domain) starting with the word 'team' (note, in your example you had teams, that's an important typo that breaks everything) is routed to the TeamController class, and the second parameter (in our example "players" is passed as $Action parameter. The TeamController itself doesn't know of the first part, it just gets the other params named as you defined it in your routes.

    This is mapped by $allowed_actions in your TeamControllerclass:

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

    and therefor directly mapped to the players method that renders the output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度