Symfony2 for Beginners - Contao Ninja · • Dependency Injection ... Symfony2 for Beginners Julius...

Click here to load reader

  • date post

    04-Jun-2018
  • Category

    Documents

  • view

    224
  • download

    0

Embed Size (px)

Transcript of Symfony2 for Beginners - Contao Ninja · • Dependency Injection ... Symfony2 for Beginners Julius...

  • Symfony2 for Beginners

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Hi, my name is

    Julius Beckmannand i work at

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • and i do:

    PHP, Symfony2 Erlang, Elixir NodeJS DevOps + Continuous Everything!

    (github|twitter).com/h4cc

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Agenda

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Agenda 1/2

    What is Symfony?

    Howto Install Develop Deploy

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Agenda 1/2

    What is Symfony?

    Howto Install Develop Deploy

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Agenda 2/2

    Plain PHP to Symfony Starting Point Dispatching Routing Symfony HTTP Foundation Symfony HTTP Kernel Symfony Routing Symfony Controller

    Extending Symfony Kernel Kernel Events Request/Response Flow (Subrequests)

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Agenda 2/2

    Plain PHP to Symfony Starting Point Dispatching Routing Symfony HTTP Foundation Symfony HTTP Kernel Symfony Routing Symfony Controller

    Extending Symfony Kernel Kernel Events Request/Response Flow (Subrequests)

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • What is Symfony?

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • What is Symfony?

    Abstract: PHP Web Application Framework At least PHP 5.3.3 By Fabien Potencier (fabpot)

    Versions: v1 since 2005 (legacy) v2 since 2011 v3 in November 2015

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • What is Symfony?

    Abstract: PHP Web Application Framework At least PHP 5.3.3 By Fabien Potencier (fabpot)

    Versions: v1 since 2005 (legacy) v2 since 2011 v3 in November 2015

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Patterns: Framework Components Modular design (Bundles) Dependency Injection Event Dispatcher

    Used by: Drupal 8 eZPublish 5 Contao 4 phpBB 3

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Patterns: Framework Components Modular design (Bundles) Dependency Injection Event Dispatcher

    Used by: Drupal 8 eZPublish 5 Contao 4 phpBB 3

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Installing Symfony

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Installer

    A tool for bootstrapping new Symfony projects.

    .Installing the installer..

    ......

    curl -LsS http://symfony.com/installer -o symfony.phar

    chmod a+x symfony.phar

    .Using the installer..

    ......

    php symfony.phar new blog

    php symfony.phar new blog 2.3

    php symfony.phar new blog 2.5.2

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Installer

    A tool for bootstrapping new Symfony projects..Installing the installer..

    ......

    curl -LsS http://symfony.com/installer -o symfony.phar

    chmod a+x symfony.phar

    .Using the installer..

    ......

    php symfony.phar new blog

    php symfony.phar new blog 2.3

    php symfony.phar new blog 2.5.2

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Installer

    A tool for bootstrapping new Symfony projects..Installing the installer..

    ......

    curl -LsS http://symfony.com/installer -o symfony.phar

    chmod a+x symfony.phar

    .Using the installer..

    ......

    php symfony.phar new blog

    php symfony.phar new blog 2.3

    php symfony.phar new blog 2.5.2

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Composer

    Manual way of bootstrapping symfony projects.

    php composer.phar create-project \symfony/framework-standard-edition \blog "2.3.*"

    This will do:

    ..1 git clone https://github.com/symfony/symfony-standard blog

    ..2 cd blog

    ..3 git checkout v2.3.X

    ..4 rm -rf .git

    ..5 composer install

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Composer

    Manual way of bootstrapping symfony projects.

    php composer.phar create-project \symfony/framework-standard-edition \blog "2.3.*"

    This will do:

    ..1 git clone https://github.com/symfony/symfony-standard blog

    ..2 cd blog

    ..3 git checkout v2.3.X

    ..4 rm -rf .git

    ..5 composer install

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Development

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Development

    .Using the PHP >=5.4 internal Webserver..

    ......

    php app/console server:run# orphp -S 127.0.0.1:8080 -t web/

    .Frontend Tools..

    ......

    php app/console assets:install php app/console assetic:dump Grunt, Gulp, Brunch

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Development

    .Using the PHP >=5.4 internal Webserver..

    ......

    php app/console server:run# orphp -S 127.0.0.1:8080 -t web/

    .Frontend Tools..

    ......

    php app/console assets:install php app/console assetic:dump Grunt, Gulp, Brunch

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Deployment

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • DeploymentPoor-Man example:.Initial..

    ......

    git clone [email protected]:your/project.git /var/wwwcd /var/www# ensure correct permissions on app/cache and app/logs

    .Deploy..

    ......

    git pullrm -f web/app_*.phpcomposer installphp app/console cache:clear --env=prod# ... more needed commands like "doctrine"

    Tools: Capifony, Ansible, SaltStack, Puppet, Chef.Automation with: Jenkins.

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

    http://capifony.org/http://jenkins.org/

  • DeploymentPoor-Man example:.Initial..

    ......

    git clone [email protected]:your/project.git /var/wwwcd /var/www# ensure correct permissions on app/cache and app/logs

    .Deploy..

    ......

    git pullrm -f web/app_*.phpcomposer installphp app/console cache:clear --env=prod# ... more needed commands like "doctrine"

    Tools: Capifony, Ansible, SaltStack, Puppet, Chef.Automation with: Jenkins.

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

    http://capifony.org/http://jenkins.org/

  • From plain PHP to Symfony

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Starting point

    GET /post.php?id=1

  • Webserver Rewrites

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Using rewrites

    GET /post/1..htaccess Configuration..

    ......RewriteEngine onRewriteRule ^post/([0-9]+)$ post.php?id=$1

    .Problems..

    ......

    Depends on Webserver Static configuration Outside of Application

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Using rewrites

    GET /post/1..htaccess Configuration..

    ......RewriteEngine onRewriteRule ^post/([0-9]+)$ post.php?id=$1

    .Problems..

    ......

    Depends on Webserver Static configuration Outside of Application

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Dispatcher and Routing

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Using dispatcher..htaccess Configuration..

    ......RewriteEngine OnRewriteRule ^(.*)$ /index.php [QSA]

    .index.php with Routing..

    ......

  • Using dispatcher..htaccess Configuration..

    ......RewriteEngine OnRewriteRule ^(.*)$ /index.php [QSA].index.php with Routing..

    ......

  • Symfony HTTP Foundation

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony HTTP Foundation

    ..1 Using HTTP as a Interface

    ..2 Hiding PHP-ServerAPI $_GET, $_POST, $_SERVER, echo(), header(), die()

    ..3 Providing Request/Response Objects

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony HTTP Foundation

    ..1 Using HTTP as a Interface

    ..2 Hiding PHP-ServerAPI $_GET, $_POST, $_SERVER, echo(), header(), die()

    ..3 Providing Request/Response Objects

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony HTTP Foundation

    ..1 Using HTTP as a Interface

    ..2 Hiding PHP-ServerAPI $_GET, $_POST, $_SERVER, echo(), header(), die()

    ..3 Providing Request/Response Objects

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Request Class

  • Symfony Response Class

  • Symfony Response Class

  • Benefits

    ..1 Reproducible Testable

    ..2 Common Interface Reuseable

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Benefits

    ..1 Reproducible Testable

    ..2 Common Interface Reuseable

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony HTTP Kernel

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Http Kernel Interface

  • HTTP Kernel

  • Symfony Routing

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Routing

    .Goal........

    Find Controller and Action for given Request.

    .Example Blog Post Route..

    ......

    # app/config/routing.yml

    blog_post_show: # Name of routepath: /post/{id} # Pattern with placeholdersdefaults:

    _controller: BlogBundle:Post:showrequirements:

    id: \d+ # Regex possible_method: GET # Ensure used method

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Routing

    .Goal........

    Find Controller and Action for given Request.

    .Example Blog Post Route..

    ......

    # app/config/routing.yml

    blog_post_show: # Name of routepath: /post/{id} # Pattern with placeholdersdefaults:

    _controller: BlogBundle:Post:showrequirements:

    id: \d+ # Regex possible_method: GET # Ensure used method

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Routing - Generate URLs.From PHP using router..

    ......

  • Symfony Routing - Generate URLs.From PHP using router..

    ......

  • Symfony Routing - Tipps.Routing to static page..

    ......

    imprint:path: /imprintdefaults:

    _controller: FrameworkBundle:Template:templatetemplate: static/imprint.html.twig

    .Routing to static redirect..

    ......

    admin_shortcut:path: /admindefaults:

    _controller: FrameworkBundle:Redirect:urlRedirectpath: /backend/administration/loginpermanent: false

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Routing - Tipps.Routing to static page..

    ......

    imprint:path: /imprintdefaults:

    _controller: FrameworkBundle:Template:templatetemplate: static/imprint.html.twig

    .Routing to static redirect..

    ......

    admin_shortcut:path: /admindefaults:

    _controller: FrameworkBundle:Redirect:urlRedirectpath: /backend/administration/loginpermanent: false

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Controller

    Request Controller Response

  • Symfony Controller

    Request Controller Response

  • post.php as Symfony Controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • PostController

  • Parameter injection

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Parameter injection

  • Templating

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Templating

  • ParamConverter Annotation

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • ParamConverter Annotation

  • Template Annotation

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Template Annotation

  • Template Annotation

  • Route Annotation

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Route Annotation

  • Extending the Symfony Kernel

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Kernel Events

    Symfony is using Events to process Requests.

    .Every Request..

    ......

    kernel.request kernel.controller kernel.response

    .Optional..

    ......

    kernel.view kernel.exception kernel.terminate

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Kernel Events

    Symfony is using Events to process Requests..Every Request..

    ......

    kernel.request kernel.controller kernel.response

    .Optional..

    ......

    kernel.view kernel.exception kernel.terminate

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Kernel Events

    Symfony is using Events to process Requests..Every Request..

    ......

    kernel.request kernel.controller kernel.response

    .Optional..

    ......

    kernel.view kernel.exception kernel.terminate

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Event kernel.request

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Resolve Controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Event kernel.controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Controller Arguments

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Call Controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Call Controller

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Event kernel.view

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Event kernel.exception

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony HttpKernel Events

  • Questions?

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony Sub-Requests

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Sub-Requests: Flow

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Sub-Requests: Example

    .Forwarding

    ..

    ......

  • Sub-Requests: Example

    .Forwarding

    ..

    ......

  • The end!

    Symfony2 for Beginners Julius Beckmann - @h4cc Silpion IT-Solutions

  • Symfony2 for BeginnersAgendaWhat is Symfony?Installing SymfonyDevelopmentDeploymentFrom plain PHP to SymfonyWebserver RewritesDispatcher and RoutingSymfony HTTP FoundationSymfony HTTP KernelSymfony RoutingSymfony Controllerpost.php as Symfony ControllerParameter injectionTemplatingParamConverter AnnotationTemplate AnnotationRoute AnnotationExtending the Symfony KernelQuestions?Symfony Sub-RequestsThe end!