Controller
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
Properties
$data |
Methods
Returns the controller that is assigned to a route
Call the controller's action specified by the $parameters array
Get a controller's action
Method that will be called before any action
Method that will be called after all actions
Returns the name of the controller without the "Controller" part
Returns a configured QueryBuilder for the corresponding model
Generates a URL from the given parameters.
Gets the browser's request
Find out whether debugging is enabled
Details
at line line 95
__construct(ParameterBag $parameters, Controller $parent = null)
at line line 110
static Controller
getController(ParameterBag $parameters)
Returns the controller that is assigned to a route
at line line 124
Response
callAction(string|null $action = null)
Call the controller's action specified by the $parameters array
at line line 149
ReflectionMethod
getAction(string|null $action = null)
Get a controller's action
at line line 199
void
setup()
Method that will be called before any action
at line line 208
void
cleanup()
Method that will be called after all actions
at line line 365
static string
getName()
Returns the name of the controller without the "Controller" part
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
at line line 396
static string
generate(string $name, mixed $parameters = array(), bool $absolute = false)
Generates a URL from the given parameters.
at line line 405
static Request
getRequest()
Gets the browser's 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
at line line 428
bool
isDebug()
Find out whether debugging is enabled