Skip to content

Instantly share code, notes, and snippets.

@NandoKstroNet
Created July 2, 2020 01:39
Show Gist options
  • Save NandoKstroNet/5c3c0ac7ad493d5a9f50c3237a8820c8 to your computer and use it in GitHub Desktop.
Save NandoKstroNet/5c3c0ac7ad493d5a9f50c3237a8820c8 to your computer and use it in GitHub Desktop.
Controller Default - Cursos Symfony 5 Mastery
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Product;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="default")
*/
public function index()
{
$name = 'Nanderson Castro';
//Criando(Inserindo) Dados com Doctrine
/*$product = new Product();
$product->setName('Produto Teste 2');
$product->setDescription('Descrição 2');
$product->setBody('Info produto 2');
$product->setSlug('produto-test-2');
$product->setPrice(3990);
$product->setCreatedAt(new \DateTime('now', new \DateTimeZone('America/Sao_Paulo')));
$product->setUpdatedAt(new \DateTime('now', new \DateTimeZone('America/Sao_Paulo')));
$manager = $this->getDoctrine()->getManager();
$manager->persist($product);
$manager->flush();*/
//Atualização
/*$product = $this->getDoctrine()->getRepository(Product::class)->find(1);
$product->setDescription('Descrição atualizada!');
$product->setPrice(3999);
$product->setUpdatedAt(new \DateTime('now', new \DateTimeZone('America/Sao_Paulo')));
$manager = $this->getDoctrine()->getManager();
$manager->flush();
*/
//Remoção
/*$product = $this->getDoctrine()->getRepository(Product::class)->find(1);
$manager = $this->getDoctrine()->getManager();
$manager->remove($product);
$manager->flush();
*/
//Buscando todos os produtos
//$products = $this->getDoctrine()->getRepository(Product::class)->findAll();
//dump($products);
//Buscando um produto especifico
// $product = $this->getDoctrine()->getRepository(Product::class)->find(2);
// print $product->getName();
// dump($product);
//Buscando um produto via slug
//$product = $this->getDoctrine()->getRepository(Product::class)->findBySlug('produto-test-2');
//dump($product);
return $this->render('index.html.twig', compact('name'));
}
/**
* @Route("/product/{slug}", name="product_single")
*/
public function product($slug)
{
return $this->render('single.html.twig', compact('slug'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment