Einführung in FLOW3/ Blog Package

  • View
    6.562

  • Download
    1

Embed Size (px)

DESCRIPTION

Einfhrung in FLOW3 mit dem Blockpackage als Referenz. Gehalten an der MFUGhttp://www.mfug.de

Transcript

  • 1. berblick ber FLOW3 mit demBlog-Package als Referenz

2. Agenda Domain Model Package Aufbau Model Validation Association Mapping Model und Repository Repository Blog Repsitory Post Repository Controller View Validation RoutingDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 2 3. Domain ModelDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 3 4. Package AufbauDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 4 5. Model NamespacenamespaceBlogDomainModel; Namespacenamespace TYPO3BlogDomainModel; Namespacenamespace TYPO3BlogDomainModel; Namespacenamespace TYPO3BlogDomainModel;Datum: 06.12.2011 FLOW3 Einfhrung / log Package www.cross-content.com @MarkusGoldbeck 5 6. TYPO3BlogDomainModel Post Namespacenamespace TYPO3BlogDomainModel; Annotationsuse DoctrineORMMapping as ORM;use TYPO3FLOW3Annotations as FLOW3; Class Post Dieses Model nach den Konzepten von DDD eine Entitiy Instanzen dieser Klasse knnen gespeichert werden/** * A blog * * @FLOW3Entity */class Post {Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck6 7. TYPO3BlogDomainModel Post Properties Variablen Deklaration als String Validierung auf Datentyp und Lnge Validierung des Datentyps innerhalb des Models und Repositories/** * The posts title. * * @var string * @FLOW3Validate(type="Text") * @FLOW3Validate(type="StringLength", options={ "minimum"=1,"maximum"=80 }) * @ORMColumn(length=80) */protected $title = ;Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 7 8. TYPO3BlogDomainModel Post Property $date Deklaration als Date Objekt Identity/*** @FLOW3Identity* @var DateTime*/protected $date; Property $blog Deklaration als Blog Objekt Association Mapping OneToMany/*** @var TYPO3BlogDomainModelBlog* @ORMManyToOne(inversedBy="posts")*/protected $blog;Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck8 9. Association Mapping OneToMany ManyToOne /** The posts contained in this blog * @var DoctrineCommonCollectionsCollection * @ORMOneToMany(mappedBy="blog") * @ORMOrderBy({"date" = "DESC"}) */protected $posts;Model BlogModel Post $posts $blog/**The blog * @var TYPO3BlogDomainModelBlog * @ORMManyToOne(inversedBy="posts") */protected $blog;Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com@MarkusGoldbeck9 10. Association Mapping ManyToMany ManyToMany/*** @var DoctrineCommonCollectionsCollection* @ORMManyToMany(inversedBy="posts")*/protected $tags;Model PostModel Tag$tags$posts/**The posts tagged with this tag* @var DoctrineCommonCollectionsCollection* @ORMManyToMany(mappedBy="tags")*/protected $posts;Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com@MarkusGoldbeck 10 11. TYPO3BlogDomainModel BlogDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck11 12. TYPO3BlogDomainModel PostDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck12 13. TYPO3BlogDomainModel Blog Setter /**Sets this blogs title * @param string $title The blogs title * @return void */ public function setTitle($title) {$this->title = $title; } Getter/**Returns the blogs title* @return string The blogs title*/public function getTitle() { return $this->title;}Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck13 14. Model und RepositoriesDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck14 15. Blog Repository Standard Blog RepositoryDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck15 16. Blog Repository Standard Methoden des Repositories add remove ndAll ndBy* (magic methods) ndOneBy* (magic methods)findBy* und findOneBy* sind sog. Magic Methods die durch das Repositoryzur Verfgung gestellt werden. Diese Methoden erlauben einen Objekte durchProperties zu nden.Das BlogRepository zum Beispiel erlaubt einem die Magic MethodenfindByDescription(foo) oder findOneByTitle(bar)auszufhren.Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck16 17. Post Repository Post Repository spezische Methode ndByBlog/*** Finds posts by the specified blog* @param TYPO3BlogDomainModelBlog $blog The blog the postmust refer to* @param integer $limit The number of posts to return at max* @return TYPO3FLOW3PersistenceQueryResultProxy The posts*/public function findByBlog(TYPO3BlogDomainModelBlog $blog){ $query = $this->createQuery(); return $query->matching($query->equals(blog, $blog)) ->setOrderings( array(date => QueryInterface::ORDER_DESCENDING)) ->execute();}Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 17 18. Post Repository Post Repository spezische Methode ndByTagAndBlog/**Finds posts by the specified tag and blog* @param TYPO3BlogDomainModelTag $tag* @param TYPO3BlogDomainModelBlog $blog The blog the postmust refer to* @return TYPO3FLOW3PersistenceQueryResultProxy The posts */public function findByTagAndBlog(TYPO3BlogDomainModelTag$tag, TYPO3BlogDomainModelBlog $blog) { $query = $this->createQuery(); return $query->matching($query->logicalAnd($query->equals(blog, $blog),$query->contains(tags, $tag)) ) ->setOrderings(array(date =>QueryInterface::ORDER_DESCENDING)) ->execute();}Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 18 19. Post Repository Post Repository spezische Methode ndRecentByBlog/*** Finds most recent posts by the specified blog** @param TYPO3BlogDomainModelBlog $blog The blog the postmust refer to* @param integer $limit The number of posts to return at max* @return TYPO3FLOW3PersistenceQueryResultProxy The posts*/public function findRecentByBlog(TYPO3BlogDomainModelBlog$blog, $limit = 5) { $query = $this->createQuery(); return $query->matching($query->equals(blog, $blog)) ->setOrderings(array(date =>QueryInterface::ORDER_DESCENDING)) ->setLimit($limit) ->execute(); }Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 19 20. Post Controller Post Controller indexAction()/*** List action for this controller. Displays latest posts* @param TYPO3BlogDomainModelTag $tag The tag to display posts for* @param TYPO3BlogDomainModelCategory $category The category to display postsfor* @return void*/public function indexAction(TYPO3BlogDomainModelTag $tag = NULL, TYPO3BlogDomainModelCategory $category = NULL) {if ($tag === NULL && $category === NULL) { $posts = $this->postRepository->findByBlog($this->blog);} elseif ($tag !== NULL) { $posts = $this->postRepository->findByTagAndBlog($tag, $this->blog); $this->view->assign(tag, $tag);} else { $posts = $this->postRepository->findByCategoryAndBlog($category, $this->blog); $this->view->assign(category, $category);}$this->view->assign(blog, $this->blog);$this->view->assign(posts, $posts);$this->view->assign(recentPosts, $this->postRepository->findRecentByBlog($this->blog));}Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck20 21. Post Controller Post Controller Dependency Injection/*** @FLOW3Inject* @var TYPO3BlogDomainRepositoryCategoryRepository*/protected $categoryRepository;/**Displays a form for creating a new post* @return void*/public function newAction() { $account = $this->findCurrentAccount(); $newPost = new TYPO3BlogDomainModelPost(); $newPost->setAuthor($account->getParty()->getName()->getFullName()); $this->view->assign(blog, $this->blog); $this->view->assign(existingPosts, $this->postRepository->findByBlog($this->blog)); $this->view->assign(categories, $this->categoryRepository->findAll()); $this->view->assign(newPost, $newPost);}Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck21 22. ViewDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck22 23. View Master Template - /Private/Layouts/Master.html

Post Template - /Private/Template/Post/Edit.html /Private/Layouts/Master.html /Private/Partials/FormErrors.html Partial Template - /Private/Partials/FormErrors.htmlDatum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 23 24. View security - /Private/Templates/Post/Index.htmlDatum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 24 25. Validation Partial Template - /Private/Partials/FormErrors.htmlObjekt Post wurde bergeben {propertyPath}: {error}Datum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck25 26. Routing Subroute Route auf das TYPO3.Blog Package /Conguration/Routes.yaml### Blog subroutes-name: BloguriPattern: defaults:@package: TYPO3.Blog@format: htmlsubRoutes:BlogSubroutes:package: TYPO3.Blog Routet auf das TYPO3.Blog Package /Packages/Application/TYPO3.Blog/Routes.yaml Erste zutreffende Regel in Routes.yaml wird ausgefhrt. nur Leerzeichen verwenden, keine Tabs.Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 26 27. Routing Package /Packages/Application/TYPO3.Blog/Routes.yaml-name: Post indexuriPattern:(posts)defaults:@package:TYPO3.Blog@controller: Post@action: index@format: html passt fr http://mfug.demo/ und http://mfug.demo/posts, da durch dieKlammern posts optional ist.Datum: 10.01.2012FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGoldbeck 27 28. Routing Package Action Routing/Packages/Application/TYPO3.Blog/Routes.yaml-name: Post actions 1uriPattern:posts/{@action}defaults:@controller: Post passt http://mfug.demo/posts/newDatum: 10.01.2012 FLOW3 Einfhrung / Blog Package www.cross-content.com @MarkusGo