in English • moved to the String component // before use Symfony\Component\Inflector\Inflector; Inflector::singularize('alumni'); // 'alumnus' Inflector::pluralize('person'); // ['persons', 'people'] // after use Symfony\Component\String\Inflector\EnglishInflector; $inflector = new EnglishInflector(); $result = $inflector->singularize('teeth'); // ['tooth'] $result = $inflector->pluralize('person'); // ['persons', 'people']
arrays because in some cases, there // are multiple valid singulars/plurals for a given word public function singularize(string $plural): array; public function pluralize(string $singular): array; }
Symfony\Component\Uid\Uuid; use Symfony\Component\Uid\Ulid; // generating a random UUID type 4 (all UUID types are supported) $uuid = Uuid::v4(); // generating a UUID Type 6 (which is not part of the standard, but it's // supported by the component because it's popular enough) $uuid = Uuid::v6(); // generating a ULID (there's only one type of them) $ulid = new Ulid();
• https://async-aws.com/ • an unofficial SDK for AWS • asynchronous by default (uses the Symfony HTTP Client) • Symfony 5.1 now uses it by default if it is installed: $ composer require async-aws/<package>
use Symfony\Component\HttpFoundation\Request; class SomeClass { public int $id; protected string $name; private ?bool $logResult; public Request $request; // ... }
Symfony\Bundle\MakerBundle\Generator: $rootNamespace: ~ # defined in MakerPass # after Symfony\Bundle\MakerBundle\Generator: $rootNamespace: !abstract defined in MakerPass
services: foo: # ... public: true tags: - { name: 'container.private', package: 'foo/bar', 'version': '1.2' } Since foo/bar 1.2: Accessing the "foo" service directly from the container is deprecated, use dependency injection instead.
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; class MainController extends AbstractController { /** * @Route("/", name="homepage", stateless=true) */ public function homepage() { // ... } }
* @Route("/new-feature", condition="env('bool:IS_FEATURE_ENABLED') === true") */ public function __invoke() { // this route will only execute when the value of the // IS_FEATURE_ENABLED env var is TRUE }
* @Annotation */ class MatchesPasswordRequirements extends Compound { protected function getConstraints(array $options): array { return [ new NotBlank(), new Type('string'), new Length(['min' => 12]), new NotCompromisedPassword(), ]; } }
use Symfony\Component\Validator\Constraints as Assert; class ServerSettings { /** * @Assert\Hostname(message="The server name must be a valid hostname.") */ protected $name; }
{ use MicroKernelTrait; protected function configureContainer(ContainerConfigurator $container): void { $container->services() ->load('App\\', '../src') ->set(Foo::class)->factory([$this, 'createFoo']); } public function createFoo(Bar $bar) { return new Foo($bar); } protected function configureRoutes(RoutingConfigurator $routes): void { $routes->add('home', '/')->controller([$this, 'helloAction']); } public function helloAction(Foo $foo) { return new Response('Hello '.get_class($foo)); } }
function testVisitingWhileLoggedIn() { $client = static::createClient(); // get or create the user somehow (e.g. creating some users only // for tests while loading the test fixtures) $userRepository = static::$container->get(UserRepository::class); $testUser = $userRepository->findOneByEmail('[email protected]'); $client->loginUser($testUser); // user is now logged in, so you can test protected resources $client->request('GET', '/profile'); $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('h1', 'Hello Username!'); }