PHP code example of thruster / view-bundle

1. Go to this page and download the library: Download thruster/view-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thruster / view-bundle example snippets



// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Thruster\Bundle\ViewBundle(),
        // ...
    );
}


// src/AppBundle/View/DefaultView.php

namespace AppBundle\View;

use Thruster\Bundle\ViewsBundle\View\View;
use AppBundle\Entity\User;

class DefaultView extends View
{
    public function welcome($name)
    {
        return [
            'msg' => 'Hello, ' . $name
        ];
    }
    
    public function me(User $user)
    {
    	  return [
    	    'name' => $user->getName(),
    	    'email' => $user->getEmail()
    	  ];
   	 }
   	 
   	 public function friend(User $friend)
   	 {
   	 	  $friend = $this->renderOne([$this, 'me'], $friend);
   	 	  // Also possible just $this->me($friend);
   	 	  
   	 	  unset($friend['email']);
   	 	  
   	 	  $friend['items'] = $this->renderMany('AppBundle:Item:public_view', $friend->getItems());
   	 	  
   	 	  return $friend;
   	 }
   	 
   	 public function friends(array $friends)
   	 {
   	 	  return [
   	 	      'data' => $this->renderMany([$this, 'friend'], $friends)
   	 	  ];
   	 }
}



namespace AppBundle\Controller;

use Thruster\Bundle\ViewsBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->jsonView('welcome', 'guest');
    }
    
    public function meAction()
    {
    	return $this->jsonView('AppBundle:Default:me', $this->getUser());
    }
    
    public function friendAction($id)
    {
    	$friend = $this->getRepository('AppBundle:User')->find($id);
    	
    	$data = $this->view('AppBundle\View\DefaultView::friend', $friend);
    	
    	return new JsonReponse($data);
    }
    
    public function friendsAction()
    {
    	$friends = $this->getRepository('AppBundle:User')->findAll();
    	
    	return $this->jsonView('friends', $friends);
    }    
}