SemanticUI
SemanticUI

Reputation: 937

Silverstripe Controller docs confusing

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?

Upvotes: 0

Views: 43

Answers (1)

wmk
wmk

Reputation: 4626

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.

Upvotes: 4

Related Questions