模块

Controller_Homepage
继承自 Controller_Template
继承自 Controller

首页控制器。

package
BootCMS
category
控制器
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 APPPATH/classes/controller/homepage.php 第 13 行声明。

属性

public boolean $auto_render

自动渲染模板

public Request $request

创建控制器的请求

public Response $response

从控制器返回的响应

public View $template

页面模板

方法

public action_index( ) (在 Controller_Homepage 中定义)

源代码

public function action_index()
{
	$sidebar = '';
	if ($this->node->sidebar)
	{
		$sidebar = Setup::makeSidebar($this->node->sidebar);
	}
	$this->template->body = View::factory('homepage')
		->bind('node', $this->node)
		->bind('sidebar', $sidebar);
}

搜索

源代码

public function action_search()
{
	$page = $this->request->query('page') > 0 ? $this->request->query('page') : 1;
	$numPerPage = 20;
	$start = $numPerPage * ( $page - 1 );
	$query = HTML::chars($this->request->query('q'));
	list($nodes, $total) = Model::factory('Node')->search($query, $start, $numPerPage);
	$pagination = Functions::page($page, $total, $numPerPage, $this->homeUrl . 'homepage/search/?q=' . $query . '&page=');
	$this->template->body = View::factory('search')
		->bind('nodes', $nodes)
		->bind('query', $query)
		->bind('pagination', $pagination);
}

public after( ) (在 Controller_Homepage 中定义)

After 方法

源代码

public function after()
{
	parent::after();
}

public before( ) (在 Controller_Homepage 中定义)

Before 方法

源代码

public function before()
{
	parent::before();
	$cache = Cache::instance();
	if (!($views = $cache->get('views-homepage', false)))
	{
		$global = BootPHP::$config->load('global');
		$views = $global->get('homepage');
		$cache->set('views-homepage', $views);
	}
	foreach ($views as $key => $view)
	{
		if (!is_array($view))
			$this->template->$key = View::factory($view);
		else
			$this->template->$key = $view;
	}
	$this->user = Auth::instance()->get_user();
	if ($this->user)
	{
		$this->template->user = $this->user;
	}
	if (!($this->node = $cache->get('homepage', false)))
	{
		$this->node = Model::factory('Node')->findByType(2);
		if (isset($this->node->id))
		{
			$cache->set('homepage', $this->node);
		}
	}
	// 设置模板变量
	$this->homeUrl = Url::base();
	$this->template->homeUrl = $this->homeUrl;
	$this->template->slug = '';
	$this->template->keywords = $this->node->keywords;
	$this->template->description = $this->node->descript;
}

public __construct( Request $request , Response $response ) (在 Controller 中定义)

创建一个新的控制器实例。每个控制器必须由创建它的请求对象来构造。

参数

  • Request $request required - 创建控制器的请求
  • Response $response required - 请求的响应

返回值

  • void

源代码

public function __construct(Request $request, Response $response)
{
	// 将请求分配给控制器
	$this->request = $request;
	// 将响应分配给控制器
	$this->response = $response;
}