.. include:: /Includes.rst.txt
Alternative route: creating a new post
======================================
After out first journey through the blog example, in this chapter we
will follow a more complex example. As an example we have chosen the
creation of a new post. The user will be offered a form in the front
end, where he can insert the title and the content of a new post
and select an existing author for this post.
After clicking the *submit* button, the list of the last posts of the
current blog should be displayed - now with the just created post at
the first place. There are multiple steps, each based on the previous
step, to be implemented that are mirrored in the actions ``new`` and
``create``. The method ``newAction()`` displays the form, while the
method ``createAction()`` really creates the post, puts it in the
repository and routes the process to the method ``indexAction()``.
Calling the method ``newAction()`` is done in our case with a link in the
front end, that looks - a bit purged - like this:
::
Create a new Post
This was created with the following Fluid code in the template
*EXT:blog_example/Resources/Private/Templates/Post/Index.html*:
::
[create another post]
The tag ```` creates a link to a special controller action
combination: ``tx_blogexample_pi1[controller]=post`` and
``tx_blogexample_pi1[action]=new``. The current blog is given as an argument
with ``tx_blogexample_pi1[blog]=12``. Because the blog cannot be sent as an object,
it must be translated into an unique identifier - the *UID*. In our case this is
the UID 12. Extbase creates the request out of these three parameters and routes
it to the according ``PostController``. The translation of the UID back to the
corresponding ``blog`` object is done automatically by Extbase.
Lets take a look at the called method ``newAction()``:
::
view->assign('authors', $this->personRepository->findAll());
$this->view->assign('blog', $blog);
$this->view->assign('newPost', $newPost);
$this->view->assign('remainingPosts', $this->postRepository->findByBlog($blog));
}
}
The method ``newAction()`` expects a ``blog`` object and an optional ``post``
object as parameter. It should be weird at first, because we have no blog and no
post object, that has to be created with the form. Actually the parameter
``$newPost`` is empty (``null``) at the first call.
Our ``PostController``, that is derived from ``ActionController``, prepares all
parameters before an action is called. The controller delegates this to an
instance of the class :php:`PropertyManager`, that has mainly two functions: it
converts the parameter from the call (from our link) into the target object and
checks if it is valid. The target for the parameter ``$blog`` is an instance of the
class :php:`\FriendsOfTYPO3\BlogExample\Domain\Model\Blog`, for the parameter
``$newPost`` it is an instance of the class
:php:`\FriendsOfTYPO3\BlogExample\Domain\Model\Post`.
How does Extbase know what the target of the conversion is? It takes this
information from the type hint of the argument. If there is nothing declared
it takes the destination type from the PHP doc above the method, from
the line:
::
* @param Blog $blog The blog the post belongs to
The link is created with the name of the argument ``$blog``.
In this way the link between the request parameter and
the ``newAction()`` is resolved.
The link parameter::
tx_blogexample_pi1[blog]=12
is assigned to the parameter::
\FriendsOfTYPO3\BlogExample\Domain\Model\Blog $blog
of the ``newAction()`` with the name "blog". With the help of the UID 12 the
corresponding blog object can be identified, reconstructed and given to the
``newAction()``.
In the first line of the ``newAction()`` the view gets an array of persons in
the parameter ``authors`` which is taken from the ``PersonRepository`` with the
``findAll()`` method. In the second and third line the view gets the parameter
``blog`` and ``newPost``. The following actions are called automatically by the
controller after calling ``newAction()``.
::
$form = $this->view()->render();
return $form;
Here you will see the shortened template *new.html*:
::
Fluid offers some comfortable tags for creating forms which names are all starting
with ``form``. The whole form is enclosed in ````. Like the creating
of a link the controller action combination which should be called when clicking the
submit button is given here.
.. note::
Don't be confused by the parameter ``method="post"``. This is the transfer method
of the form and has nothing to do with our domain (instead of ``method="post"``
it also could be ``method="get"``).
The form is bound with ``object="{newPost}"`` to the object that we have assigned to
the variable ``newPost`` in the controller. The specific form fields have a property
``property="..."```. With this a form field can be filled with the content of the
property of the given object. Because ``{newPost}`` is empty (= ``null``) here, the
form fields are empty at first.
The ``select`` tag is created by the Fluid tag ````.
The available options are taken by Fluid from the content
of the given property ``options="{authors}"``. In our case it is an array with all
persons of the ``PersonRepository``. The visible text of the options are created by
Fluid from the parameter ``optionLabelField="fullName"``. The created HTML code of
the form looks like this:
::
TYPO3 takes the rendered form and includes it at the appropriate place in the HTML page
(see figure 3-5).
.. figure:: /Images/3-BlogExample/figure-3-5.png
:align: center
Figure 3-5: The rendered form
Clicking the *submit* button calls the ``createAction`` of the ``PostController``.
Here you will see the stripped-down method:
::
/**
* Creates a new post
*
* @param Blog $blog The blog the post belogns to
* @param Post $newBlog A fresh Blog object which has not yet been added to the repository
* @return void
*/
public function createAction(Blog $blog, Post $newPost)
{
// TODO access protection
$blog->addPost($newPost);
$newPost->setBlog($blog);
$this->addFlashMessage('created');
$this->redirect('index', null, null, ['blog' => $blog]);
}
The arguments ``$blog`` and ``$post`` are filled and validated equivalent to the
method ``newAction()``.
.. note::
During the conversion of the arguments into the property values of the target
object, the above-mentioned ``PropertyManager`` checks if any errors are encountered
during the validation. The validation effected on the base of the property
definitions of the target object. More about the subject validating you will find
in the section :ref:`validating-domain-objects`.
The post is added to the blog with ``$blog->addPost($newPost)``. After that the
following processing is forwarded by ``$this->redirect([...])`` to the method
``indexAction()``. Thereby the blog - now with the new post - is passed as
argument. In order that the new post is available in the blog when next called, it
must be persisted. This is done automatically after the flow through the extension
in the dispatcher of Extbase.
.. note::
What's ``redirect()``? Extbase knows the methods ``forward()`` and ``redirect()``.
They both forward the further processing. The difference is that
``redirect()`` starts a complete new page call (new request response cycle),
while ``forward()`` resides in the processing of the current page call. The
outcome of this is an important consequence: At ``redirect()`` the changes are
persisted before the call of the target action, whereas at ``forward()`` these
must be done by hand with the call of
:php:`$persistenceManager->persistAll();`.