If this tutorial is helpful to you, you can donate some money to the tutorial author via Paypal.

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 id and the title for the location.

Like I was recently told FLOW3 does not need the id. If you don't have an id the object mapper is generating one for you. I add the id anyway because at the moment I have no idea if I will need it for some reason later or not.

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

Generate the model 

./flow3 kickstart:model Layh.Events Location name:string

Layh.Events/Classes/Domain/Model/Location.php 

<?php
namespace Layh\Events\Domain\Model;
 
use TYPO3\FLOW3\Annotations as FLOW3;
 
/**
 * A Location
 *
 * @FLOW3\Scope("prototype")
 * @FLOW3\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

Generate your repository 

./flow3 kickstart:repository Layh.Events Location

Layh.Events/Classes/Domain/Repository/LocationRepository 

<?php
namespace Layh\Events\Domain\Repository;
 
/**
 * LocationRepository
 *
 * Repository for locations
 */
  
class LocationRepository extends \TYPO3\FLOW3\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

Layh.Events/Classes/Controller/LocationController.php 

<?php
namespace Layh\Events\Controller;
 
use TYPO3\FLOW3\Annotations as FLOW3;
 
/**
 * Location controller for the Layh.Events package
 *
 * @FLOW3\Scope("singleton")
 */
class LocationController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
 
    /**
     * @var Layh\Events\Domain\Repository\LocationRepository
     * @FLOW3\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.
Layh.Events/Resources/Private/Templates/Location/Add.html 

<f:layout name="Index.html" />
 
<f:section name="content">
    <h1>FLOW3 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.textbox property="name" /><br />
            <f:form.submit value="Save" />
        </f:form>
    </div>
 
</f:section>

Layh.Events/Resources/Private/Templates/Location/Index.html 

<f:layout name="Index.html" />
 
<f:section name="content">
    <h1>FLOW3 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.
Layh.Events/Resources/Configuration/Routes.yaml 

--
    name: 'Location routes'
    uriPattern: 'location/{@action}'
    defaults:
      @package: Events
      @controller: Location
      @format: 'html'
 
--
    name: 'Action route'
    uriPattern: '{@action}'
    defaults:
      @package: Events
      @controller: Event
      @format: 'html'
 
--
  name: 'Homepage'
  uriPattern: ''
  defaults:
    @package: Events
    @controller: Event
    @format: 'html'
    @action: 'index'

 


Was this tutorial helpful? So, you can donate some money to the tutorial author via Paypal.