Skip to content

Instantly share code, notes, and snippets.

View anvodev's full-sized avatar

An Vo anvodev

View GitHub Profile
@anvodev
anvodev / HttpKernel.php
Last active May 3, 2020 17:38
controller arguments stage
//vendor/symfony/http-kernel/HttpKernel.php
<?php
//...
// controller arguments
$arguments = $this->argumentResolver->getArguments($request, $controller);
$event = new ControllerArgumentsEvent($this, $controller, $arguments, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
$controller = $event->getController();
@anvodev
anvodev / PostController.php
Last active May 3, 2020 18:20
get a post with or without comments
<?php
namespace App\Controller;
use App\Entity\Post;
use App\Review;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@anvodev
anvodev / HttpKernel.php
Last active May 3, 2020 15:31
finish handling the request and return the response
//vendor/symfony/http-kernel/HttpKernel.php
<?php
private function handleRaw(Request $request, int $type = self::MASTER_REQUEST): Response
{
//...previous stages
// call controller
$response = $controller(...$arguments);
// view
@anvodev
anvodev / index.php
Created May 3, 2020 17:00
Symfony 5 flow in index.php
<?php
//public/index.php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
@anvodev
anvodev / HttpKernel.php
Created May 3, 2020 17:24
Request stage
//vendor/symfony/http-kernel/HttpKernel.php
<?php
//...
$this->requestStack->push($request);
// request
$event = new RequestEvent($this, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);
@anvodev
anvodev / HttpKernel.php
Created May 3, 2020 17:29
Load controller stage
<?php
//...
// load controller
if (false === $controller = $this->resolver->getController($request)) {
throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
}
$event = new ControllerEvent($this, $controller, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
@anvodev
anvodev / Lucky.php
Last active May 5, 2020 01:31
A Service to get lucky number
<?php
// src/Lucky.php
namespace App;
class Lucky
{
public function getNumber()
{
return random_int(1,100);
@anvodev
anvodev / DefaultController.php
Created May 5, 2020 01:29
use Lucky service in a controller to return a lucky number
<?php
// src/Controller/DefaultController.php
namespace App\Controller;
use App\Lucky;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
@anvodev
anvodev / services.yaml
Created May 5, 2020 15:49
default services.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
@anvodev
anvodev / Customer.php
Created May 12, 2020 18:59
Customer Entity with default autogenerate ID
<?php
namespace App\Entity;
use App\Repository\CustomerRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CustomerRepository::class)
*/