3.5.2 Atributy controllerů
Pro kompletní seznam atributů controlleru a jejich popisu navštivte CakePHP API zde: http://api.cakephp.org/1.2/class_controller.html.
3.5.2.1 $name
Uživatelé PHP4 by měli začít definici controlleru použitím atributu $name. Tento atribut nastavuje jméno controlleru. Obvykle je to množné číslo primárního modelu který controller spravuje.
<?php
# $name controller attribute usage example
class RecipesController extends AppController {
var $name = 'Recipes';
}
?>
<?php# $name controller attribute usage exampleclass RecipesController extends AppController {var $name = 'Recipes';}?>
3.5.2.2 $components, $helpers and $uses
Další nejčastěji používané atributy říkají controlleru, který helper, komponenta nebo model budete používat spolu s aktuálním controllerem. Použítí těchto atributů umožní tyto MVC třídy používat jako proměnné třídy ( např. $this->ModelName).
Každý controller má některé z těchto tříd přístupné defaultně, proto nepotřebujete konfigurovat controller pro všechny.
Controllery mají přístup k jejich primárnímu modelu defaultně. RecipesController má přístupný model Recipe přes $this->Recipe, ProductsController podobně zprístupní model Product přes $this->Product.
Html, Form a Session Helpery jsou též přístupné defaultně. Podobně i SessionComponent. Pro bližší pochopení těchto tříd si pročtěte pozdější sekce v tomto manuálu.
Pojďme se podívat, jak říct controlleru, že chcete využít dalších MVC tříd.
<?php
class RecipesController extends AppController {
var $name = 'Recipes';
var $uses = array('Recipe', 'User');
var $helpers = array('Ajax');
var $components = array('Email');
}
?>
<?phpclass RecipesController extends AppController {var $name = 'Recipes';var $uses = array('Recipe', 'User');var $helpers = array('Ajax');var $components = array('Email');}?>
Každá z těchto proměnných je spojena s jejich dědičnou hodnotou, proto není nezbytné (např.) redeklarovat Form Helper nebo cokoliv, co je již deklarováno ve Vašem App controleru.
3.5.2.3 Page-related Attributes: $layout and $pageTitle
A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout.
The $layout attribute can be set to the name of a layout saved in /app/views/layouts. You specify a layout by setting $layout equal to the name of the layout file minus the .ctp extension. If this attribute has not been defined, CakePHP renders the default layout, default.ctp. If you haven’t defined one at /app/views/layouts/default.ctp, CakePHP’s core default layout will be rendered.
<?php
// Using $layout to define an alternate layout
class RecipesController extends AppController {
function quickSave() {
$this->layout = 'ajax';
}
}
?>
<?php// Using $layout to define an alternate layoutclass RecipesController extends AppController {function quickSave() {$this->layout = 'ajax';}}?>
You can also change the title of the page (that is located in the bar at the top of your browser) using $pageTitle. In order for this to work properly, your layout needs to include the $title_for_layout variable, at least between the <title> and </title> tags in the head of the HTML document.
<?php
// Using $pageTitle to define the page title
class RecipesController extends AppController {
function quickSave() {
$this->pageTitle = 'My search engine optimized title';
}
}
?>
<?php// Using $pageTitle to define the page titleclass RecipesController extends AppController {function quickSave() {$this->pageTitle = 'My search engine optimized title';}}?>
You can also set the page title from the view using $this->pageTitle (You must include the $this-> part.) This is recommended, as it better separates the logic from the layout and content. For a static page you must use $this->pageTitle in the view if you want a custom title.
If $this->pageTitle is not set, a title will be automatically generated based on the controller name, or the view file name in the case of a static page.
3.5.2.4 The Parameters Attribute ($params)
Controller parameters are available at $this->params in your CakePHP controller. This variable is used to provide access to information about the current request. The most common usage of $this->params is to get access to information that has been handed to the controller via POST or GET operations.
3.5.2.4.1 form
$this->params['form']
Any POST data from any form is stored here, including information also found in $_FILES.
3.5.2.4.2 admin
$this->params['admin']
Is set to 1 if the current action was invoked via admin routing.
3.5.2.4.3 bare
$this->params['bare']
Stores 1 if the current layout is empty, 0 if not.
3.5.2.4.4 isAjax
$this->params['ajax']
Stores 1 if the current request is an ajax call, 0 if not. This variable is only set if the RequestHandler Component is being used in the controller.
3.5.2.4.5 controller
$this->params['controller']
Stores the name of the current controller handling the request. For example, if the URL /posts/view/1 was requested, $this->params['controller'] would equal "posts".
3.5.2.4.6 action
$this->params['action']
Stores the name of the current action handling the request. For example, if the URL /posts/view/1 was requested, $this->params['action'] would equal "view".
3.5.2.4.7 pass
$this->params['pass']
Stores the GET query string passed with the current request. For example, if the URL /posts/view/?var1=3&var2=4 was requested, $this->params['pass'] would equal "?var1=3&var2=4".
3.5.2.4.8 url
$this->params['url']
Stores the current URL requested, along with key-value pairs of get variables. For example, if the URL /posts/view/?var1=3&var2=4 was called, $this->params['url'] would contain:
[url] => Array
(
[url] => posts/view
[var1] => 3
[var2] => 4
)
3.5.2.4.9 data
$this->data
Used to handle POST data sent from the FormHelper forms to the controller.
// The FormHelper is used to create a form element:
$form->text('User.first_name');
// The FormHelper is used to create a form element:$form->text('User.first_name');
Which when rendered, looks something like:
<input name="data[User][first_name]" value="" type="text" />
When the form is submitted to the controller via POST, the data shows up in this->data
//The submitted first name can be found here: $this->data['User']['first_name'];
//The submitted first name can be found here:$this->data['User']['first_name'];
3.5.2.4.10 prefix
$this->params['prefix']
Set to the routing prefix. For example, this attribute would contain the string "admin" during a request to /admin/posts/someaction.
3.5.2.4.11 named
$this->params['named']
Stores any named parameters in the url query string in the form /key:value/. For example, if the URL /posts/view/var1:3/var2:4 was requested, $this->params['named'] would be an array containing:
[named] => Array
(
[var1] => 3
[var2] => 4
)
3.5.2.5 Other Attributes
While you can check out the details for all controller attributes in the API, there are other controller attributes that merit their own sections in the manual.
The $cacheAction attribute aids in caching views, and the $paginate attribute is used to set pagination defaults for the controller. For more information on how to use these attributes, check out their respective sections later on in this manual.
