class Controller

The Controller class represents a bunch of pages relating to the same subject (Model in most cases) - for example, there is a PlayerController, a TeamController and a HomeController.

Controllers contain special methods called 'actions', which are essentially different pages performing different actions - for example, the TeamController might contain a 'show' action, which renders the team's page, and a 'new' action, which renders the page that is shown to the user when they want to create a new team.

Actions have some unique characteristics. Take a look at this sample action:

public function showAction(Request $request, Team $team) {
  return array('team' => $team);
}

The following route will make sure that showAction() handles the request:

team_show:
  pattern:    /teams/{team}
  defaults: { _controller: 'Team', _action: 'show' }

First of all, the method's name should end with Action. The parameters are passed dynamically, and the order is insignificant.

You can request Symfony's Request or Session class, or even a model, which will be generated based on the route parameters. For example, the route pattern /posts/{post}/comments/{commentId} (note how you can use both comment and commentId as parameters - just make sure to use the correct variable name on the method later) and can be used with actions like these:

public function sampleAction (Request $request, NewsArticle $post, Comment $comment)

public function sampleAction (NewsArticle $post, Session $session, Request $request, Comment $comment)

A method's return value can be: - Symfony's Response Class - A string representing the text you want the user to see - An array representing the variables you want to pass to the controller's view, so that it can be rendered

Traits

ContainerAwareTrait

Properties

$data

Methods

__construct(ParameterBag $parameters, Controller $parent = null)

No description

static Controller
getController(ParameterBag $parameters)

Returns the controller that is assigned to a route

Response
callAction(string|null $action = null)

Call the controller's action specified by the $parameters array

getAction(string|null $action = null)

Get a controller's action

void
setup()

Method that will be called before any action

void
cleanup()

Method that will be called after all actions

static string
getName()

Returns the name of the controller without the "Controller" part

static QueryBuilder
getQueryBuilder($type = null)

Returns a configured QueryBuilder for the corresponding model

static string
generate(string $name, mixed $parameters = array(), bool $absolute = false)

Generates a URL from the given parameters.

static Request
getRequest()

Gets the browser's request

static Player
getMe()

Gets the currently logged in player

bool
isDebug()

Find out whether debugging is enabled

Details

at line line 95
__construct(ParameterBag $parameters, Controller $parent = null)

Parameters

ParameterBag $parameters The array returned by $request->attributes
Controller $parent The controller who invoked this controller

at line line 110
static Controller getController(ParameterBag $parameters)

Returns the controller that is assigned to a route

Parameters

ParameterBag $parameters The array returned by $request->attributes

Return Value

Controller The controller

at line line 124
Response callAction(string|null $action = null)

Call the controller's action specified by the $parameters array

Parameters

string|null $action The action name to call (e.g. show), null to invoke the default one

Return Value

Response The action's response

at line line 149
ReflectionMethod getAction(string|null $action = null)

Get a controller's action

Parameters

string|null $action The action name to call (e.g. show), null to invoke the default one

Return Value

ReflectionMethod The action method

at line line 199
void setup()

Method that will be called before any action

Return Value

void

at line line 208
void cleanup()

Method that will be called after all actions

Return Value

void

at line line 365
static string getName()

Returns the name of the controller without the "Controller" part

Return Value

string

at line line 381
static QueryBuilder getQueryBuilder($type = null)

Returns a configured QueryBuilder for the corresponding model

The returned QueryBuilder will only show models visible to the currently logged in user

Parameters

$type

Return Value

QueryBuilder

at line line 396
static string generate(string $name, mixed $parameters = array(), bool $absolute = false)

Generates a URL from the given parameters.

Parameters

string $name The name of the route
mixed $parameters An array of parameters
bool $absolute Whether to generate an absolute URL

Return Value

string The generated URL

at line line 405
static Request getRequest()

Gets the browser's request

Return Value

Request

at line line 418
static Player getMe()

Gets the currently logged in player

If the user is not logged in, a Player object that is invalid will be returned

Return Value

Player

at line line 428
bool isDebug()

Find out whether debugging is enabled

Return Value

bool