Introducing Symfony
Composer, Bundles, Symfony Framework
Table of Contents
• Introducing Symfony
• Install Symfony
– Symfony Installer
– Composer
• Symfony Overview
2
INTRODUCING SYMFONY
Advantages of The Framework
Introducing Symfony
• MVC Architecture
• Scalability
• Great Set of Components
• Active community
• Good documentation
4
INSTALL & CONFIGURE SYMFONY
Symfony Installer & Composer
Download XAMPP
Composer
• To install Composer go to
https://getcomposer.org/download/
and follow instructions.
Install Scoop
Install Symfony
• Install Symfony: go to https://symfony.com/download
9
Symfony Installer
• To install, execute:
symfony new --full my_project_name
Run Symfony
cd my_project_name/
symfony serve
11
Run Symfony
SYMFONY OVERVIEW
First Steps With The Framework
13
The Bundle System
• What is a Bundle?
– set of files within a directory designed to implement
a single feature
– each bundle has its own namespace
\<Vendor Name>\(<Namespace>\)*<Class Name>
$ php bin/console generate:bundle
14
The Bundle System
• The bundle class name must follow five simple rules:
– Use only alphanumeric characters and underscores;
– Use a CamelCase name;
– Use a descriptive and short name (no more than two words);
– Prefix the name with the concatenation of the vendor
(and optionally the category namespaces);
– Suffix the name with Bundle.
15
The Bundle System
• Example of valid bundle names:
YAML - YAML Ain’t Markup Language!
• YAML is:
•human-readable data serialization language like json and xml, but
easier to read
•based on key: value used for Symfony configuration
17
Directory Structure
Directory Description
Application Kernel (AppKernel) and main
app/
configuration files in config/ directory
bin/ Symfony console
src/ the project’s code
tests/ automatic(unit) tests
18
Directory Structure
Directory Description
var/ logs and cache files
vendor third-party dependecies (bundles)
src/ the project’s code
web/ web root directory, application index
Symfony Project Overview
• src/Kernel.php
– Is the most important file of our project
– All bundles are registered/activated in Kernel
class Kernel extends BaseKernel
{
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
}
}
Symfony Project Overview
• src/Kernel.php
– AppKernel also provides configuration for different environments
class Kernel extends BaseKernel {
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
}
}
Each environment should have its own config file suffixed with the name of the provided
environment, e.g.: config_dev.yml
Each environment creates its own cache and log files in var/cache and var/log
Symfony Project Overview
• app/config/routes.yaml
– Contains mapping between controllers and routes
# This file is the entry point to configure your own HTTP routes.
# Files in the routes/ subdirectory configure the routes for your dependencies.
#index:
# path: /
# defaults: { _controller: 'App\Controller\DefaultController::index' }
Symfony Project Overview
• app/config/packages/security.yml
– Authentication and authorization (access control) settings.
Settings for password encoding, login form, roles, role hierarchy
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users_in_memory: { memory: null }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
provider: users_in_memory
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
Symfony Project Overview
• app/config/services.yml
– Configuration for different services and bundles
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
Symfony Project Overview
• app/config/services.yml
– Configuration for different services and bundles
• Full project configuration:
– php bin/console config:dump-reference
Symfony Project Overview
• src/
– Source folder of our Controllers, Entities, Repositories
Symfony Project Overview
• vendor/
– Directory of all dependencies and
external libraries used in our project.
– Autoloading and dependencies are
handled by Composer so we don’t
need to do anything in this directory.
Summary
Install Composer and Symfony Installer
The Bundle system
YAML
Install Symfony and project structure
28