1. Go to this page and download the library: Download gnugat/marshaller-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/ */
gnugat / marshaller-bundle example snippets
// File: app/AppKernel.php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Gnugat\MarshallerBundle\GnugatMarshallerBundle(),
);
// ...
}
// ...
}
// File: src/AppBundle/Entity/Article.php
namespace AppBundle\Entity;
class Article
{
public function __construct($title, $content)
{
$this->title = $title;
$this->content = $content;
}
public function getTitle()
{
return $this->title;
}
public function getContent()
{
return $this->content;
}
}
// File: src/AppBundle/Marshaller/ArticleMarshaller.php
use AppBundle\Entity\Article;
use Gnugat\Marshaller\MarshallerStrategy;
class ArticleMarshaller implements MarshallerStrategy
{
public function supports($toMarshal, $category = null)
{
return $toMarshal instanceof Article;
}
public function marshal($toMarshal)
{
return array(
'title' => $toMarshal->getTitle(),
'content' => $toMarshal->getContent(),
);
}
}
// File: src/AppBundle/Controller/ArticleController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
class ArtcileController extends Controller
{
/**
* @Route("/api/v1/articles")
* @Method({"GET"})
*/
public function listAction()
{
$articles = $this->get('app.article_repository')->findAll();
return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshalCollection($articles), 200);
}
/**
* @Route("/api/v1/articles/{id}")
* @Method({"GET"})
*/
public function viewAction(Article $article)
{
return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshal($article), 200);
}
}
array('title' => 'Nobody expects...');
// File: src/AppBundle/Marshaller/ArticleMarshaller.php
use AppBundle\Entity\Article;
use Gnugat\Marshaller\MarshallStrategy;
class PartialArticleMarshaller implements MarshallStrategy
{
public function supports($toMarshal, $category = null)
{
return $toMarshal instanceof Article && 'partial' === $category;
}
public function marshal($toMarshal)
{
return array(
'title' => $toMarshal->getTitle(),
);
}
}
// File: src/AppBundle/Controller/ArticleController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
class ArtcileController extends Controller
{
/**
* @Route("/api/v1/articles")
* @Method({"GET"})
*/
public function listAction()
{
$articles = $this->get('app.article_repository')->findAll();
return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshalCollection($articles, 'partial'), 200);
}
/**
* @Route("/api/v1/articles/{id}")
* @Method({"GET"})
*/
public function viewAction(Article $article)
{
return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshal($article), 200);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.