Symfony Framework Getting Started Guide: Quick Setup and Core Concepts
Symfony PHP framework is powerful, flexible and scalable, but its steep learning curve often discourages newbies. This article will guide you to quickly get started with Symfony, and you can easily build a fully functional website even if you only have the basic knowledge of PHP and HTML and the basic concepts of modern website development.
Quick build
Recommended to download the Symfony Standard version without vendor packages. Unzip to your website root directory (for example: f:wwwrsywx_test). Next, download the PHP package management tool Composer. If you have cURL installed, you can use the following command:
curl -S https://getcomposer.org/installer | php
Otherwise, use:
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
This will download the composer.phar file. Run the following command to install the necessary Bundles:
php composer.phar update
(The above directory structure is for reference only, the .hg directory is a version control directory and may not exist)
If your web server (such as Apache) is configured correctly, you can now access the site (development environments usually use app_dev.php as the entry).
Bundle, controller, view, model (entity)
Symfony is based on Bundle (similar to modules in other frameworks). A Bundle is a collection of files that handles specific features of a website. A Bundle contains controllers, views, and entity files (models), forming the basis of the MVC structure.
Create a Bundle with the following command:
php app/console generate:bundle
Enter information such as Bundle namespace, name, target directory, configuration format (it is recommended to use YAML) according to the prompts.
Route
Routing mechanism maps HTTP requests to Bundle/functions that handle the request. Symfony supports beautiful URIs. It is recommended to define routes in the routing.yml file of Bundle (for example: path-to-your-site-root/src/tr/rsywxBundle/Resources/config).
Database
This guide uses a simple database (such as MariaDB or MySQL). You can create databases and tables using third-party tools such as PhpMyAdmin. Then, configure the app/config/parameters.yml file to connect to the database:
parameters: database_driver: pdo_mysql database_host: 127.0.0.1 database_port: null database_name: symfony database_user: root database_password: null
Import database structure:
php app\console doctrine:mapping:import
Generate entity:
php app\console doctrines:generate:entity tr
(where tr is the namespace of the Bundle)
Summary
This guide introduces the rapid construction and core concepts of the Symfony framework, including Bundle, controller, views, models, and database configuration. The follow-up guide will explain how to create routes, controllers, entities/repositories and templates to make your website really work.
FAQ
composer create-project symfony/framework-standard-edition my_project_name
I hope this guide will help you get started with Symfony quickly! For more details, please refer to the official Symfony documentation.
The above is the detailed content of Building a Web App with Symfony 2: Bootstrapping. For more information, please follow other related articles on the PHP Chinese website!