控制器方法(Controller Methods),接着上面继续说哈!

来源:互联网 发布:设计师必知的100 编辑:程序博客网 时间:2024/05/16 01:31
控制器方法的完整列表和它们的描述CakePHPAPI访问。


交互视图


控制器与视图以多种方式进行交互。首先,他们能够将数据传递给视图,使用set()。你也可以决定使用哪一个视图类,从控制器和视图文件应该呈现。


Controller::set(string $var, mixed $value)



set()方法的主要方式从控制器发送数据到你的观点。一旦你设置(),使用变量可以访问你的观点:

// First you pass data from the controller:$this->set('color', 'pink');// Then, in the view, you can utilize the data:?>You have selected <?php echo $color; ?> icing for the cake.

set()方法以关联数组作为第一个参数。这往往是一个快速的方法来分配一组信息视图。


1.3版本的变化:数组键将不再变形之前分配给视图(“underscored_key”不能成为“underscoredKey”了,等等)。

$data = array(    'color' => 'pink',    'type' => 'sugar',    'base_price' => 23.95);// make $color, $type, and $base_price// available to the view:$this->set($data);


属性pageTitle美元不再存在,使用set()来设置标题:

$this->set('title_for_layout', 'This is the page title');


控制器:渲染(字符串视图中,美元字符串$布局)


render()方法结束时自动调用每个请求的控制器动作。该方法执行所有视图逻辑(使用中给出的数据你使用set()方法),地方视图内美元布局和服务给最终用户。


默认视图文件渲染是由所使用的约定。如果搜索()请求RecipesController的行动时,视图文件在/app/View/Recipes/search.ctp将呈现:

class RecipesController extends AppController {// ...    public function search() {        // Render the view in /View/Recipes/search.ctp        $this->render();    }// ...}


0 0
原创粉丝点击