Slide 1

Slide 1 text

The Hitchhiker's Guide to the Web Development Carlos Hernando @chernando

Slide 2

Slide 2 text

1,220,000,000 results

Slide 3

Slide 3 text

DON’T PANIC

Slide 4

Slide 4 text

PHP Python Ruby Java JavaScript ?

Slide 5

Slide 5 text

Model View Controller

Slide 6

Slide 6 text

Model View Controller

Slide 7

Slide 7 text

Object-Relational Mapping

Slide 8

Slide 8 text

Object-Relational Mapping Post.comments SELECT * FROM comment where post_id = 2;

Slide 9

Slide 9 text

Model View Controller

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Model View Controller

Slide 13

Slide 13 text

http://www.ticketea.com/try-it-general http://www.ticketea.com/ URL Controller: Index URL Controller: event ‘try-it’

Slide 14

Slide 14 text

GET POST HTTP REQUEST

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Config CLI Tools Module/Application URLs

Slide 17

Slide 17 text

Model View Controller Template MVT

Slide 18

Slide 18 text

Model Template View

Slide 19

Slide 19 text

class Post(models.Model): title = models.CharField(max_length=45) author = models.ForeignKey(AUTH_USER_MODEL) content = models.TextField()

Slide 20

Slide 20 text

class Comment(models.Model): author = models.ForeignKey(AUTH_USER_MODEL) post = models.ForeignKey(Post) content = models.TextField()

Slide 21

Slide 21 text

Model Template View

Slide 22

Slide 22 text

{% extends "base.html" %} {% block content %} {% for post in posts %}

{{ post.title }}

by {{ post.author }}

{{ post.content }}

{{ post.comments }} comments

{% endfor %} {% endblock %}

Slide 23

Slide 23 text

class Post(models.Model): title = models.CharField(max_length=45) author = models.ForeignKey(AUTH_USER_MODEL) content = models.TextField() @property def comments(self): return self.comment_set.count() def __unicode__(self): return self.title def get_absolute_url(self): return reverse('post', args=[self.pk]) ;-(

Slide 24

Slide 24 text

class PostForm(forms.ModelForm): class Meta: model = Post exclude = ('author',) {{ form.as_p }} {% csrf_token %} forms.py post_create.html

Slide 25

Slide 25 text

Model Template View

Slide 26

Slide 26 text

from . import views urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^publish/$', views.post_create, name='publish'), url(r'^post/(?P\d+)/$', views.post_detail, name='post'), url(r'^post/(?P\d+)/comment/$', views.comment_create, name='comment'), )

Slide 27

Slide 27 text

def index(request): last_posts = Post.objects\ .order_by('-pk')[:5] context = {'posts': last_posts} return render(request, 'blog/post_list.html', context)

Slide 28

Slide 28 text

{% extends "base.html" %} {% block content %} {% for post in posts %}

{{ post.title }}

by {{ post.author }}

{{ post.content }}

{{ post.comments }} comments

{% endfor %} {% endblock %}

Slide 29

Slide 29 text

def comment_create(request, post_id): post = get_object_or_404(Post, pk=post_id) form = CommentForm(request.POST or None) if form.is_valid(): comment = Comment() comment.author = request.user comment.post = post comment.content=form.cleaned_data['content'] comment.save() return HttpResponseRedirect( post.get_absolute_url()) else: return render(request, 'blog/post_detail.html', {'post': post, 'form': form})

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Config CLI Tools Module/Bundle Controller Model Views and Routes

Slide 32

Slide 32 text

Model View Controller

Slide 33

Slide 33 text

/** * Post * * @ORM\Table() * @ORM\Entity */ class Post { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="title", type="string", length=45) */ private $title;

Slide 34

Slide 34 text

/** * @ORM\OneToMany(targetEntity="Comment", mappedBy="post") */ private $comments; /** * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments") * @ORM\JoinColumn(name="post_id", referencedColumnName="id") */ private $post; Post.php Comment.php

Slide 35

Slide 35 text

Model View Controller

Slide 36

Slide 36 text

{% extends "base.html.twig" %} {% block content %} {% for post in posts %}

{{ post.title }}

{{ post.content }}

{{ post.comments.count }} comments

{% endfor %} {% endblock %}

Slide 37

Slide 37 text

$post = new Post(); $form = $this->createFormBuilder($post) ->add('title', 'text') ->add('content', 'textarea') ->getForm(); {{ form_widget(form) }} PostController.php detail.html.twig

Slide 38

Slide 38 text

Model View Controller

Slide 39

Slide 39 text

try_it_blog_homepage: pattern: / defaults: { _controller: TryItBlogBundle:Post:index } try_it_blog_post_create: pattern: publish/ defaults: { _controller: TryItBlogBundle:Post:create } try_it_blog_post_detail: pattern: post/{post_id}/ defaults: { _controller: TryItBlogBundle:Post:detail } try_it_blog_comment_create: pattern: post/{post_id}/comment/ defaults: { _controller: TryItBlogBundle:Comment:create }

Slide 40

Slide 40 text

class PostController extends Controller { public function indexAction() { $posts = $this->getDoctrine() ->getRepository('TryItBlogBundle:Post') ->findBy(array(), // ALL array('id' => 'desc'), 7); return $this->render( 'TryItBlogBundle:Post:index.html.twig', array('posts' => $posts)); }

Slide 41

Slide 41 text

public function createAction(Request $request) { /* $post and $form missing */ if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { $em = $this->getDoctrine() ->getManager(); $em->persist($post); $em->flush(); return $this->redirect($this ->generateUrl('try_it_blog_post_detail', array('post_id' => $post->getId()))); } } return $this->render( 'TryItBlogBundle:Post:create.html.twig', array('form' => $form->createView()));

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

AJAX RESTful API

Slide 44

Slide 44 text

Micro framework VS Framework

Slide 45

Slide 45 text

Model View Controller

Slide 46

Slide 46 text

Models.Comment = Backbone.Model.extend({ }); Collections.Comments = Backbone.Collection.extend({ model: Models.Comment }); Model Collection

Slide 47

Slide 47 text

Models.Post = Backbone.Model.extend({ defaults: { title: 'No title', content: 'No content', comments: new Collections.Comments() }, initialize: function() { this.get('comments').on('all', function() { this.trigger('change') }, this); } }); See fetch!

Slide 48

Slide 48 text

Model View Controller

Slide 49

Slide 49 text

Templates.Post = _.template([ '

{{ title }}

', '

', '{{ content }}', '

', '

', ' Show comments', ' Comment!', '

', ].join(''));

Slide 50

Slide 50 text

Model View Controller

Slide 51

Slide 51 text

Views.Post = Backbone.View.extend({ initialize: function() { this.model.on('all', this.render, this); this.render(); }, render: function() { this.$el.html( Templates.Post(this.model.toJSON())); var n_comments = this.model.get('comments') .length; this.$el.append($('

') .html('' + n_comments + ' comments')); return this; },

Slide 52

Slide 52 text

events: { 'click .showComments': 'showComments', 'click .comment': 'createComment' }, showComments: function() { var comments = $('#comments'); var body = comments.find('.modal-body').empty(); this.model.get('comments').each( function(comment) { body.append( Templates.Comment(comment.toJSON())); }); comments.modal('show'); }, createComment: function() { var view = new Views.CreateComment( { post: this.model }); },

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Routes?

Slide 55

Slide 55 text

PHP Python Ruby Java JavaScript MVC

Slide 56

Slide 56 text

HTML5 / CSS3 Bootstrap / Boilerplate Responsive Accessibility Developer’s tools Sessions Migrations Database performance NoSQL Cache Load balance Deploy Scalability Security ...

Slide 57

Slide 57 text

Web Development https://www.udacity.com/course/cs253 CS169.1x: Software as a Service https://www.edx.org/courses/BerkeleyX/CS169.1x/2013_March/about HTML5 Game Development https://www.udacity.com/course/cs255

Slide 58

Slide 58 text

42

Slide 59

Slide 59 text

https://github.com/chernando/ https://speakerdeck.com/chernando/hhgttwd

Slide 60

Slide 60 text

So Long, and Thanks for All the Fish

Slide 61

Slide 61 text

ABOUT This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/ 3.0/. Product names, logos and trademarks of other companies which are referenced in this document remain the property of those other companies.