In this part we don’t learn anything new, but it is required to have it for the next step. So normally an event has some location where the event takes place. So we create a new model called Location and the corresponding repository called LocationRepository.
The location model
For now we keep it simple, so all we need for now for the location model is the title for the location.
So build the model, it will look similar to the event model. Add the getter and setter functions and don’t forget to set the annotations.
Generate the model
1 |
./flow kickstart:model Layh.Events Location name:string |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace Layh\Events\Domain\Model; use TYPO3\Flow\Annotations as Flow; /** * A Location * * @Flow\Scope("prototype") * @Flow\Entity */ class Location { /** * The name * @var string */ protected $name; /** * Get the Location's name * * @return string The Location's name */ public function getName() { return $this->name; } /** * Sets this Location's name * * @param string $name The Location's name * @return void */ public function setName($name) { $this->name = $name; } } |
The location repository
If you have any problems with this part, check back on the part where we create the repository for the events. It is exactly the same here.
Generate your repository
1 |
./flow kickstart:repository Layh.Events Location |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace Layh\Events\Domain\Repository; use TYPO3\Flow\Annotations as Flow; /** * LocationRepository * * Repository for locations * @Flow\Scope("singleton") */ class LocationRepository extends \TYPO3\Flow\Persistence\Repository { } |
Creating the rest of the files required
At this point we have to do some more work. Now we do the same stuff we have done for the events for the locations. We add a action to display a form, we use this form to add locations, and we add the action to save the locations.
Create the LocationController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace Layh\Events\Controller; use TYPO3\Flow\Annotations as Flow; /** * Location controller for the Layh.Events package * * @FLOW3\Scope("singleton") */ class LocationController extends \TYPO3\Flow\Mvc\Controller\ActionController { /** * @var Layh\Events\Domain\Repository\LocationRepository * @Flow\Inject */ protected $locationRepository; /* * @return void */ public function indexAction() { $locationList = $this->locationRepository->findAll(); $this->view->assign('locations', $locationList); } /** * @return void */ public function addAction() { } /** * createAction * * @param \Layh\Events\Domain\Model\Location $location * @return void */ public function createAction(\Layh\Events\Domain\Model\Location $location) { $this->locationRepository->add($location); $this->redirect('index'); } } |
The templates
Now we have to create two templates. One for the indexAction and one for the addAction.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<f:layout name="Index.html" /> <f:section name="content"> <h1>TYPO3 Flow Tutorial Add Location</h1> <div id="content"> <f:form action="create" controller="Location" package="Layh.Events" name="location" object="{location}"> <label>Title:</label><f:form.textfield property="name" /><br /> <f:form.submit value="Save" /> </f:form> </div> </f:section> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<f:layout name="Index.html" /> <f:section name="content"> <h1>TYPO3 Flow Tutorial Location List</h1> <ul> <f:for each="{locations}" as="loc"> <li>{loc.name}</li> </f:for> </ul> </f:section> |
Updating Routes.yaml
Since we have a new Controller now, we add this controller to the Routes.yaml to have some nice URLs.
Modifiy your Routes.yaml so that it looks like below. We add the new routes for the locationController at the top.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
-- name: 'Location routes' uriPattern: 'location/{@action}' defaults: @package: Layh.Events @controller: Location @format: 'html' -- name: 'Action route' uriPattern: '{@action}' defaults: @package: Layh.Events @controller: Event @format: 'html' -- name: 'Homepage' uriPattern: '' defaults: @package: Layh.Events @controller: Event @format: 'html' @action: 'index' |