Navigation

Controllers and Actions

Zed’s communication layer is the entry point to the system. Here are executed all of the external requests from users, the Yves-application and all command-line calls. The purpose of this layer is to retrieve the data, delegate to the business layer and to handover it to the presentation layer.

The methods inside of a controller are called actions. Usually an action reads the incoming request-data, maybe validates it with a form, delegates it to a facade from the business layer and writes the response.

The following controller provides two actions: indexAction() and testAction().

<?php
namespace Namespace\Zed\Bundle\Communication\Controller;
 
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
 
class IndexController extends AbstractController
{
    public function indexAction()
    {
        return $this->viewResponse([]);
    }
 
    public function testAction(Request $request)
    {
       return $this->viewResponse([]);
    }
}

Input Parameters

You can define an input parameter $request that is automatically injected. This is a Symfony\Component\HttpFoundation\Request, so you will find methods to fetch the get and post parameters and the session.

<?php
public function testAction(Request $request)
{
    $request->getSession(); // retrieve current session
    $request->query->get('a'); // retrieve get-parameter index?a=
    // ...
}

Response Methods

There are several ways to send responses in different use cases. These methods are called at the end of the action and their return value is returned by the action. e.g. return $this->redirectResponse('/my-module').

Response Method Purpose

viewResponse(array $data = [])

Loads the related twig template and provides the data for the template.

jsonResponse($data = null, $status = 200, $headers = [])

Sends a json response which is needed for ajax calls.

redirectResponse($url, $status = 302, $headers = [])

Performs a redirect to the given relative path or absolute url.

streamedResponse($callback = null, $status = 200, $headers = [])

Sends binary data. This is used to draw the state machines.

Example:

<?php
public function testAction(Request $request)
{
    // ...
    return $this->viewResponse(['customer' => ['name' => 'John doe']]);
}

Now the data can be accessed in the twig template like this: {{customer.name}} .


Controllers and Paths in Zed

Every module-controller-action triple is related to a URL of the application. Usually a URL has three segments which are related to module, controller and action.

Examples of paths:

Module Controller Action Url(s)

MyModule

IndexController

indexAction()

my-module/

MyModule

IndexController

testAction()

my-module/index/test

MyModule

AnyOtherController

indexAction()

my-module/any-other

MyModule

AnyOtherController

testAction()

my-module/any-other/test

Cart

GatewayController

addItemAction()

cart/gateway/add-item

As you can see there are some path-resolver rules:

  • The term index is automatically used, when the path is not fully specified.
  • CamelCase class and method names are presented with a dash-separator.
  • Suffixes like Controller and Action are removed.

Mapping Twig Templates

Each action returning $this->viewResponse() must have a dedicated Twig template:

  • Controller and action:
    MyNamespace/Zed/MyModuleCommunication/Controller/TestController::doSomethingAction()
  • Expected path of template:
    MyNamespace/Zed/MyModule/Presentation/Test/do-something.twig