Symfony Demo Application
repository·main·Indexed 25 days ago
https://github.com/symfony/demoA reference implementation demonstrating development following Symfony Best Practices. It includes a complete project structure with entities for Users, Posts, Tags, and Comments, as well as custom CLI commands for user management (app:add-user, app:delete-user, app:list-users) and repository methods for searching and retrieving posts.
What's inside symfony-demo
- The Bootswatch Flatly theme is included in this project with modifications to prevent it from automatically loading Google Fonts. Instead of using the standard CSS, the project uses the embedded Sass source and overrides the font imports using Sass variables. This allows you to use the Flatly theme while maintaining control over your font assets.
Install the Symfony Demo Application
mainYou can install the Symfony Demo project using either the Symfony CLI or Composer.
Using Symfony CLI
Run the following command to create a new project based on the demo:
symfony new --demo my_projectUsing Composer
To create a new project via Composer:
composer create-project symfony/symfony-demo my_projectAlternatively, you can clone the repository and install dependencies manually:
git clone https://github.com/symfony/demo.git my_project cd my_project/ composer installRun the Symfony Demo Application
mainThe application requires no prior configuration. You can run it using the Symfony CLI, a standard web server (Nginx/Apache), or the built-in PHP web server.
Using Symfony CLI
cd my_project/ symfony serveBy default, the application will be accessible at
https://localhost:8000.Using the built-in PHP web server
cd my_project/ php -S localhost:8000 -t public/Run tests in the Symfony Demo Application
mainTo execute the project's test suite, run the PHPUnit binary located in the
bin/directory.cd my_project/ ./bin/phpunitInstall dependencies for the Symfony Demo project
mainIf you encounter errors indicating that dependencies are missing when running the application, ensure you have installed the project dependencies using Composer.
Run the following command in the project root:
composer installRequirements for Symfony Demo Application
mainEnsure your environment meets the following requirements:
- PHP 8.2.0 or higher
- PDO-SQLite PHP extension enabled
- The usual Symfony application requirements
Install Symfony Runtime if missing
mainIf the application fails with an error stating that the Symfony Runtime is missing, you need to require the
symfony/runtimepackage via Composer.composer require symfony/runtimeRetrieve latest posts with `PostRepository::findLatest()`
mainUsefindLatest()to fetch a paginated list of published posts. You can optionally filter the results by a specificTagobject. The method automatically filters for posts wherepublishedAtis less than or equal to the current time and orders them by publication date in descending order.Search for posts with `PostRepository::findBySearchQuery()`
mainUsefindBySearchQuery()to search for posts by their title. The method splits the provided string into individual terms (ignoring terms shorter than 2 characters) and performs anORsearch across the titles. Results are ordered bypublishedAtdescending.User entity role constants
mainThe
App\u005cEntity\u005cUserclass defines constants for user roles to ensure consistency across the application and prevent typos when performing security checks.final public const ROLE_USER = 'ROLE_USER'; final public const ROLE_ADMIN = 'ROLE_ADMIN';Reference: app:delete-user command
mainTheapp:delete-usercommand is used to delete users from the database.Delete a user via the CLI
mainThe
app:delete-usercommand allows you to remove a user from the database by providing their username.Usage Modes:
- Direct Argument: Provide the username directly in the command.
- Interactive Mode: If you omit the username argument, the command will launch an interactive wizard and prompt you to enter the username.
If the specified username does not exist in the database, the command will throw a
RuntimeExceptionstating:User with username "<username>" not found.