本文整理汇总了PHP中FOS\RestBundle\View\View::setTemplateVar方法的典型用法代码示例。如果您正苦于以下问题:PHP View::setTemplateVar方法的具体用法?PHP View::setTemplateVar怎么用?PHP View::setTemplateVar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FOS\RestBundle\View\View
的用法示例。
在下文中一共展示了View::setTemplateVar方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getView
public function getView($msg, $code)
{
$data1 = new Response($msg, $code);
$view = new View($data1);
$view->setTemplate('AppBundle:message.html.twig');
$view->setTemplateVar('data');
return $view;
}
示例2: getClaimedByUserAction
/**
* @ApiDoc(
* section="Claims",
* description="Display the claimed requests of a user",
* output="AppBundle\Controller\ClaimController",
* parameters={
* {"name"="id", "dataType"="integer", "required"=true, "description"="The user's id"}
* }
* )
*/
public function getClaimedByUserAction($id)
{
$rep = $this->getDoctrine()->getRepository('AppBundle:Claim');
$claims = $rep->findClaimedByUser($id);
$view = new View($claims, 200);
$view->setTemplate('AppBundle:Claim:claimed.html.twig');
$view->setTemplateVar('claimeds');
return $this->handleView($view);
}
示例3: getUsersAction
/**
* @ApiDoc(
* section="Admin/Users",
* description="Display all the users",
* output="AppBundle\Controller\Admin\AdminUserController",
* )
*/
public function getUsersAction()
{
$rep = $this->getDoctrine()->getRepository('AppBundle:User');
$users = $rep->findAll();
$view = new View($users);
$view->setTemplate('AppBundle:User:users.html.twig');
$view->setTemplateVar('users');
return $this->handleView($view);
}
示例4: getCategoryItemsAction
/**
* @ApiDoc(
* section="Categories",
* description="Display the creation form of categories",
* output="AppBundle\Controller\CategoryController",
* parameters={
* {"name"="cat", "dataType"="string", "required"=true, "description"="Category's name"},
* }
* )
*/
public function getCategoryItemsAction($cat)
{
$rep = $this->getDoctrine()->getRepository('AppBundle:Category');
$category = $rep->findForItem($cat);
$view = new View($category);
$view->setTemplate('AppBundle:Category:items.html.twig');
$view->setTemplateVar('items');
//dump($view);die();
return $this->handleView($view);
}
示例5: indexAction
/**
* Lists all Article entities.
*
*/
public function indexAction()
{
//echo phpinfo(); die;
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MwRestBundle:Article')->findAll();
$view = new View();
$view->setData($entities);
$view->setTemplateVar('entities');
$view->setTemplate('MwRestBundle:Article:index.html.twig');
return $this->container->get('fos_rest.view_handler')->handle($view);
}
示例6: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例7: getUserAction
/**
* @ApiDoc(
* section="Users",
* description="Display the creation form of the user",
* output="AppBundle\Controller\UserController",
* parameters={
* {"name"="id", "dataType"="integer", "required"=true, "description"="The user's id"}
* },
* statusCodes={
* 404="Returned when someone"
* }
* )
*/
public function getUserAction($id)
{
// On enlève les informations sensibles
$self = $this->getUser();
$user = null;
if ($self->hasRole('ROLE_ADMIN')) {
$rep = $this->getDoctrine()->getRepository('AppBundle:User');
$user = $rep->find($id);
} else {
if ($self->hasRole('ROLE_USER')) {
$user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findForUsers($id);
}
}
if (!$user) {
throw new HttpException(404, 'user not found');
}
$view = new View($user);
$view->setTemplate('AppBundle:User:details.html.twig');
$view->setTemplateVar('details');
return $this->handleView($view);
}
示例8: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->enableMaxDepthChecks();
$view->setSerializationContext($context);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例9: getItemsByUserAction
/**
* @ApiDoc(
* section="Items",
* description="Display the items of an user",
* output="AppBundle\Controller\ItemController",
* parameters={
* {"name"="id", "dataType"="integer", "required"=true, "description"="The item's id"},
* },
* statusCodes={
* 404="Returned when the items didn't exist"
* }
* )
*/
public function getItemsByUserAction($id)
{
$rep = $this->getDoctrine()->getRepository('AppBundle:Item');
$items = $rep->findBy(array('itemuserid' => $id));
if (!$items) {
$view = $this->get('raiponce')->getView('Resources not Found!', 404);
return $this->handleView($view);
}
$view = new View($items, 200);
$view->setTemplate('AppBundle:Item:items.html.twig');
$view->setTemplateVar('items');
return $this->handleView($view);
}
示例10: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return false;
}
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->forceView) {
return;
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
$context = $view->getContext();
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context->addGroups($configuration->getSerializerGroups());
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context->setMaxDepth(0);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
if (!empty($vars)) {
$parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
if ($template && !$view->getTemplate()) {
if ($template instanceof TemplateReferenceInterface) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $this->viewHandler->handle($view, $request);
$event->setResponse($response);
}