本文整理匯總了PHP中Craft\Craft::createComponent方法的典型用法代碼示例。如果您正苦於以下問題:PHP Craft::createComponent方法的具體用法?PHP Craft::createComponent怎麽用?PHP Craft::createComponent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Craft\Craft
的用法示例。
在下文中一共展示了Craft::createComponent方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: createCommand
/**
* @param string $name command name (case-insensitive)
*
* @return \CConsoleCommand The command object. Null if the name is invalid.
*/
public function createCommand($name)
{
$name = StringHelper::toLowerCase($name);
$command = null;
if (isset($this->commands[$name])) {
$command = $this->commands[$name];
} else {
$commands = array_change_key_case($this->commands);
if (isset($commands[$name])) {
$command = $commands[$name];
}
}
if ($command !== null) {
if (is_string($command)) {
$className = 'NerdsAndCompany\\Schematic\\ConsoleCommands\\' . IOHelper::getFileName($command, false);
return new $className($name, $this);
} else {
// an array configuration
return Craft::createComponent($command, $name, $this);
}
} elseif ($name === 'help') {
return new \CHelpCommand('help', $this);
} else {
return;
}
}
示例2: actionGetElements
/**
* Returns the requested elements as JSON
*
* @param callable|null $configFactory A function for generating the config
* @param array|null $config The API endpoint configuration
*
* @throws Exception
* @throws HttpException
*/
public function actionGetElements($configFactory = null, array $config = null)
{
if ($configFactory !== null) {
$params = craft()->urlManager->getRouteParams();
$variables = isset($params['variables']) ? $params['variables'] : null;
$config = $this->_callWithParams($configFactory, $variables);
}
// Merge in default config options
$config = array_merge(['paginate' => true, 'pageParam' => 'page', 'elementsPerPage' => 100, 'first' => false, 'transformer' => 'Craft\\ElementApi_ElementTransformer'], craft()->config->get('defaults', 'elementapi'), $config);
if ($config['pageParam'] == 'p') {
throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.');
}
if (!isset($config['elementType'])) {
throw new Exception('Element API configs must specify the elementType.');
}
/** @var ElementCriteriaModel $criteria */
$criteria = craft()->elements->getCriteria($config['elementType'], ['limit' => null]);
if (!empty($config['criteria'])) {
$criteria->setAttributes($config['criteria']);
}
// Load Fractal
$pluginPath = craft()->path->getPluginsPath() . 'elementapi/';
require $pluginPath . 'vendor/autoload.php';
$fractal = new Manager();
$fractal->setSerializer(new ArraySerializer());
// Define the transformer
if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) {
$transformer = $config['transformer'];
} else {
Craft::import('plugins.elementapi.ElementApi_ElementTransformer');
$transformer = Craft::createComponent($config['transformer']);
}
if ($config['first']) {
$element = $criteria->first();
if (!$element) {
throw new HttpException(404);
}
$resource = new Item($element, $transformer);
} else {
if ($config['paginate']) {
// Create the paginator
require $pluginPath . 'ElementApi_PaginatorAdapter.php';
$paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']);
// Fetch this page's elements
$criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1);
$criteria->limit = $config['elementsPerPage'];
$elements = $criteria->find();
$paginator->setCount(count($elements));
$resource = new Collection($elements, $transformer);
$resource->setPaginator($paginator);
} else {
$resource = new Collection($criteria, $transformer);
}
}
JsonHelper::sendJsonHeaders();
echo $fractal->createData($resource)->toJson();
// End the request
craft()->end();
}