Published on

The slides I used for my presentation on the Symfony2 PHP framework at the MemphisPHP meetup.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • After I finished studying and successfully passed the ZCE exam I knew I needed to move beyond playing around with WordPress, and after seeing a presentation by Ryan Weaver, I  settled on learning Symfony2. Part of this decision was motivated by the fact that Symfony2 has embraced the very same changes in PHP introduced in 5.3 that I had studied for the ZCE, and did not want to forget... things like:  namespaces, closures, and anonymous functions. Per the official SensioLabs definition: " Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP components that solve common web development problems ."
  • And if you utilize Symfony's tight integration with Doctrine2 Object Relational Mapper, it fits the Model-View-Controller pattern.
  • Properties translate more or less to columns in a given MySQL table. These properties "or columns" are largely what you would expect them to be. In the first example of the Shopper model we have: Integer, string, string. There are special relationships, however, and these are the ones that tie these objects together, so that if you have one object you may easily query related objects (or rows) from other tables or even the same table.
  • This is the jist of the Shopper entity. It's missing the getters and setters, but those can be generated automatically via a console command. The last property of Shopper, orders, is a special property... it represents a special relationship between itself and another entity, in this case, the order entity. Whereas a string will be stored in the realname and streetaddress properties, the orders property will instead contain an array of the orders placed by the Shopper at one point or another. In this particular case a join column called shopper_id will be placed on the Orders table.
  • You can easily use these built in queries for most purposes, but it is important to know that you can also create your own custom queries using Doctrine's version of SQL syntax called DQL. If you do create your own, you can abstract them into a repository class so that the methods are accessible by the same means as these generic ones.
  • Previously I mentioned console commands that Symfony2 provides. These are just a few, and I do mean a few, of my favorites.
  • Twig is the templating engine utilized by Symfony2. This is the "view" part of the Model-View-Controller design pattern. It's syntax is very similar to the Python templating engine Jinja. It features some cool things such as template inheritance.  You can see that if you were to render this particular template, it would also be rendering all of base.html.twig, which presumably contains a reference to a block called navigation which this child template is overriding.
  • This is a very basic example configuration of a route. It is utilizing a yml file, and provides optional requirements parameters that ensure that the route is only matched if the request utilized the GET HTTP method, and the end of the URL matches the regular expression which, in this case, only matches digits.
  • This example assumes that we have passed from our controller to the view a shopper object fetched from the database via doctrine.
  • This controller is about as simple as it gets. It uses the wildcard in the URL which was passed to the showAction function, grabs the shopper object from the database, and immediately passes it to the view.
  • So long as you've namespaced your classes using either t he technical interoperability standards for PHP 5.3, or the PEAR naming convention for class you can now use Symfony2's universal class loader to load your classes automatically when you use them. 
  • Symfony2

    1. 1. Symfony2, etc. presented by @mdpatrick / Dan Patrick
    2. 2. MVC or Model-View-Controller <ul><li>  </li></ul>
    3. 3. An Example of a Model Scenario :  We have an Order that is placed by a Shopper.  Each Order contains a number of Items (that were bought) . <ul><li>Shopper  has properties:  id (primary key), realname,   streetaddress, and orders. </li></ul><ul><li>Order has properties: id (primary key), productlist, total, and purchaser. </li></ul><ul><li>Item has properties: id (primary key), name, weight, and price. </li></ul>
    4. 4. <ul><li>class Shopper </li></ul><ul><li>{ </li></ul><ul><li>    /** </li></ul><ul><li>      * @ORMColumn(type=&quot;string&quot;, length=&quot;100&quot;, nullable=false) </li></ul><ul><li>      */ </li></ul><ul><li>    protected $realName; </li></ul><ul><li>    /** </li></ul><ul><li>      * @ORMColumn(type=&quot;string&quot;, lenght=&quot;255&quot;, nullable=false) </li></ul><ul><li>      */ </li></ul><ul><li>    protected $streetaddress; </li></ul><ul><li>    /** </li></ul><ul><li>      * @ORMOneToMany(targetEntity=&quot;Order&quot;, inversedBy=&quot;user&quot;) </li></ul><ul><li>      */ </li></ul><ul><li>    protected $orders; </li></ul><ul><li>}  </li></ul>
    5. 5. Fetching Objects (or rows) From Their Respective Tables <ul><li>From the Controller: </li></ul><ul><li>$shopperRepository = $this->get('doctrine') </li></ul><ul><li>         ->getRepository('DansBundle:Shopper'); </li></ul><ul><li>An Array of ALL Shopper Objects: </li></ul><ul><li>$shopperRepository->findAll() </li></ul><ul><li>Return A Single Object of Shopper Entity: </li></ul><ul><li>$shopperRepository->findOneByRealName('Dan Patrick'); </li></ul><ul><li>Return A Single Object by Primary Key: </li></ul><ul><li>$shopperRepository->find($id) </li></ul>
    6. 6. Cool Console Commands <ul><li>$  php console generate:bundle </li></ul><ul><li>$  php console router:debug </li></ul><ul><li>$  php console container:debug </li></ul><ul><li>Doctrine Related </li></ul><ul><li>$ php console doctrine:query:sql </li></ul><ul><li>$  php console doctrine:generate:entities </li></ul><ul><li>$  php console doctrine:generate:entity </li></ul><ul><li>$  php console doctrine:generate:crud </li></ul>
    7. 7. Twig Templating Engine <ul><li>{% extends &quot;base.html&quot; %} </li></ul><ul><li>{% block navigation %} </li></ul><ul><li>    <ul id=&quot;navigation&quot;> </li></ul><ul><li>    {% for item in navigation %} </li></ul><ul><li>        <li> </li></ul><ul><li>            <a href=&quot;{{ item.href }}&quot;> </li></ul><ul><li>                {% if 2 == item.level %}&nbsp;&nbsp;{% endif %} </li></ul><ul><li>                {{ item.caption|upper }} </li></ul><ul><li>            </a> </li></ul><ul><li>        </li> </li></ul><ul><li>    {% endfor %} </li></ul><ul><li>    </ul> </li></ul><ul><li>{% endblock navigation %} </li></ul>
    8. 8. A Routing Example <ul><li># app/config/routing.yml   </li></ul><ul><li>shopper_show:  </li></ul><ul><li>         pattern: /shopper/{id} </li></ul><ul><li>         defaults: { _controller: DansBundle:User:show } </li></ul><ul><li>         requirements: </li></ul><ul><li>                 _method: GET </li></ul><ul><li>                 id: d+ </li></ul>
    9. 9. Generating Links In The View By Utilizing Route Names <ul><li><a href=&quot;{{ path('shopper_show', {'id': }) }}&quot;>Shopper</a> </li></ul><ul><li>Assuming our shopper object has the primary key of 3, this would render as... </li></ul><ul><li><a href=&quot;/user/3&quot;>Shopper</a> </li></ul>
    10. 10. A Look At The Controller ... triggered by GET to /user/3 <ul><li>class UserController extends Controller </li></ul><ul><li>{ </li></ul><ul><li>    public function showAction($id) </li></ul><ul><li>     { </li></ul><ul><li>        $shopper = $this->get('doctrine')-> </li></ul><ul><li>                 getRepository('DansBundle:Shopper')->find($id); </li></ul><ul><li>        return $this->render('DansBundle:User:show.html.twig', </li></ul><ul><li>                 array('shopper' => $id)); </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
    11. 11. Autoloading via Universal Class Loader <ul><li>No more require(), require_once(), include(), etc.  </li></ul><ul><li>Instead *just*.... </li></ul><ul><li>use MyVendorMyClass; </li></ul><ul><li>$myvar = new MyClass(); </li></ul>