Symfony Documentation
repository·7.4·Indexed 25 days ago
https://github.com/symfony/symfony-docsOfficial documentation for the Symfony framework, featuring guides, component references, tutorials, and best practices for application structure, service configuration, and controller implementation.
What's inside symfony-docs
- Symfony uses the Twig template engine. While Twig provides its own set of default filters and functions, Symfony provides additional extensions (filters, functions, and tags) specifically designed to integrate Symfony components directly into your Twig templates. If the built-in Symfony extensions do not meet your needs, you can create a custom Twig extension.
Overview of the Form Component
7.4The Form component provides a way to create, process, and reuse forms. It is designed to handle data interaction between your client and your application. While traditionally used with HTML forms, the component is data-agnostic and can process data from standard form posts or from APIs.Overview of Symfony project structure
7.4A standard Symfony project contains the following key directories:
bin/: Contains executable files likebin/console.config/: Contains configuration for routes, services, and packages.public/: The document root for the project; contains publicly accessible files.src/: Contains all your PHP application code.templates/: Contains Twig templates.var/: Stores automatically-created files likevar/cache/andvar/log/.vendor/: Contains third-party libraries installed via Composer.
Overview of the TypeInfo component
7.4The TypeInfo component is used to extract type information from PHP elements. It supports:
- Type Definition: A powerful
Typedefinition capable of handling unions, intersections, and generics. - Element Extraction: Methods to retrieve types from PHP properties, method arguments, return types, and raw strings.
- Type Definition: A powerful
Benefits of migrating from flat PHP to Symfony
7.4Migrating from a custom 'flat PHP' architecture to Symfony provides several architectural advantages:
- Code Organization: Promotes clear, consistent, and reusable code structures that improve developer productivity.
- Reduced Boilerplate: You no longer need to maintain low-level utilities like autoloading, routing, or controller rendering; Symfony handles these core concerns.
- Component Ecosystem: Immediate access to high-quality open-source tools including:
Doctrine(ORM)Twig(Templating engine)SecuritycomponentFormcomponentValidatorcomponentTranslationcomponent
- Flexible Routing: The Routing component enables fully-flexible URL structures.
- HTTP-Centric Architecture: Built-in support for HTTP caching (via Symfony's internal HTTP cache) and compatibility with external tools like
Varnish.
Understand the Symfony configuration directory structure
7.4Symfony applications use the
config/directory to manage application settings. The default structure includes:config/bundles.php: Enables or disables packages (bundles) in your application.config/packages/: Stores configuration for every installed package (e.g.,api_platform.yaml).config/routes.yamlorconfig/routes/: Defines the application routing configuration.config/services.yaml: Configures the service container.config/preload.php: Defines classes for OPcache preloading.config/reference.php: (Introduced in Symfony 7.4) An autogenerated file containing definitions to improve IDE autocompletion and static analysis when using PHP for configuration.
When using Symfony Flex, installing a package automatically updates
bundles.phpand creates new configuration files inconfig/packages/.What is a Controller in Symfony
7.4A controller is a PHP callable (typically a method within a class) that processes aRequestand returns aResponse. TheResponsecan be HTML, JSON, XML, a file download, a redirect, or an error. The controller contains the application logic required to render the content of a page.What is the Service Container
7.4In Symfony, useful objects (like a Mailer or a database saver) are called services. All these services live inside a central object called the service container. The container centralizes object construction, promotes strong architecture, and optimizes performance by managing how these objects are instantiated and shared.Overview of available Lock Stores
7.4Locks are managed by
Stores, which implementPersistingStoreInterface. Stores can be local or remote, and may or may not support blocking, expiration, sharing, or serialization. If a store does not support blocking, theLockclass will automatically retry to acquire the lock in a non-blocking way until successful.Special Testing Stores
InMemoryStore(LOCK_DSN=in-memory): Saves locks in memory during a process.NullStore(LOCK_DSN=null): Does not persist anything (introduced in Symfony 7.2).
What is a Gateway Cache?
7.4A gateway cache (also known as a reverse proxy cache, surrogate cache, or HTTP accelerator) sits between your application and the client.
Its workflow is:
- Accept requests from the client.
- Pass requests to your application.
- Receive responses from your application.
- Store responses deemed "cacheable".
- If the same resource is requested again, the cache sends the stored response directly to the client, bypassing your application.
Map form fields to object properties using property_path
7.4By default, a form field named
fieldNamemaps to thefieldNameproperty of the underlying object using thePropertyAccesscomponent (supportingget*,set*,is*, andhas*methods).You can customize this mapping using the
property_pathoption:- Different Property Name: Map a field named
deadlineto thedueDateproperty. - Nested Properties: Use dot notation to map to nested objects, e.g.,
category.nameto access thenameproperty of aCategoryobject returned bygetCategory().
// Mapping to a different property $builder->add('deadline', DateType::class, [ 'property_path' => 'dueDate', ]); // Mapping to nested properties $builder->add('categoryName', TextType::class, [ 'property_path' => 'category.name', ]);- Different Property Name: Map a field named
Use Entity Value Resolvers for Doctrine entities
7.4If using Doctrine, you can use theEntityValueResolverto automatically query for an entity based on a route variable and pass it directly to your controller action. If the entity is not found, it automatically triggers a 404 error. For more complex retrieval logic, perform the query manually using a Doctrine repository method inside the controller.