模块

Controller_Product
继承自 Controller_Template
继承自 Controller

产品控制器。

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

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

属性

public boolean $auto_render

自动渲染模板

public Request $request

创建控制器的请求

public Response $response

从控制器返回的响应

public View $template

页面模板

方法

public action_entry( ) (在 Controller_Product 中定义)

加载产品节点

源代码

public function action_entry()
{
	$productId = (int) $this->request->param('id');
	$product = $this->model->load($productId);
	for ($i = 1; $i < 11; $i++)
	{
		$item = 'item' . $i;
		$product->$item = unserialize($product->$item);
	}
	// 解析图片
	$oMedia = Model::factory('Media');
	$product->pictures = unserialize($product->pictures);
	$pics = array();
	if (is_array($product->pictures) && count($product->pictures))
	{
		foreach ($product->pictures as $pic)
		{
			$media = $oMedia->load($pic);
			$pics[] = array($media->file_name, $media->thumb_name);
		}
	}
	$product->pictures = $pics;
	$categories = Model::factory('product_category')->findByOrder();
	$this->template->title = $product->product_name . ' - ' . $this->template->title;
	$this->template->body = View::factory('product/entry')
		->bind('categories', $categories)
		->bind('node', $product);
}

public action_index( ) (在 Controller_Product 中定义)

所有产品的页面

源代码

public function action_index()
{
	$cateId = $this->request->param('id') ? (int) $this->request->param('id') : 1;
	$oMedia = Model::factory('Media');
	$categories = Model::factory('product_category')->findByOrder();
	$products = $this->model->findByCategory($cateId);
	foreach ($products as $k => $p)
	{
		$pics = unserialize($p->pictures);
		$p->thumb = '';
		if (is_array($pics) && count($pics))
		{
			$media = $oMedia->load($pics[0]);
			$media->thumb_name && $p->thumb = $media->thumb_name;
		}
	}
	$this->template->body = View::factory('product/archives')
		->bind('categories', $categories)
		->bind('products', $products);
}

搜索

源代码

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

public after( ) (在 Controller_Product 中定义)

After 方法

源代码

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

public before( ) (在 Controller_Product 中定义)

Before 方法

源代码

public function before()
{
	parent::before();
	$cache = Cache::instance();
	if (!($views = $cache->get('views', false)))
	{
		$global = BootPHP::$config->load('global');
		$views = $global->get('views');
		$cache->set('views', $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;
	}
	$this->model = Model::factory('Product');
	$this->homeUrl = Url::base();
	$this->template->title = '产品';
	$this->template->homeUrl = $this->homeUrl;
}

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;
}