4 PHP MVC Frameworks Introducing Symfony
4 PHP MVC Frameworks Introducing Symfony
• Introducing Symfony
• Install Symfony
– Symfony Installer
– Composer
• Symfony Overview
2
INTRODUCING SYMFONY
• MVC Architecture
• Scalability
• Great Set of Components
• Active community
• Good documentation
4
INSTALL & CONFIGURE SYMFONY
• To install Composer go to
https://getcomposer.org/download/
and follow instructions.
Install Scoop
Install Symfony
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
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
14
The Bundle System
15
The Bundle System
• 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
18
Directory Structure
Directory Description
• 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
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
• 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
28