Tutorial: Hello World
Edit on GitHubThis tutorial is also available on the Spryker Training website. For more information and hands-on exercises, visit the Spryker Training website.
Challenge description
Show the Hello World! string on a web page on your browser. To do so, build a new module called HelloWorld
.
Build the HelloWorld module
The steps described in this procedure describe how to build a new module. To add the HelloWorld
module, do the following:
- Go to
/src/Pyz/Yves/
and add a new module called HelloWorld.
A new module is simply a new folder.
- Add a new controller for the module:
- In
HelloWorld
, add a new folder calledController
. - Add the controller class called
IndexController
:
- In
<?php
namespace Pyz\Yves\HelloWorld\Controller;
use Spryker\Yves\Kernel\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class IndexController extends AbstractController
{
/**
* @param Request $request
*
* @return \Spryker\Yves\Kernel\View\View
*/
public function indexAction(Request $request)
{
$data = ['helloWorld' => 'Hello World!'];
return $this->view(
$data,
[],
'@HelloWorld/views/index/index.twig'
);
}
}
- Add the route to the controller:
- In
HelloWorld
, add a new folder calledPlugin
. - In
Plugin
, add a folder called Provider. - Add your
RouteProviderPlugin
class with the nameHelloWorldRouteProviderPlugin
:
- In
<?php
namespace Pyz\Yves\HelloWorld\Plugin\Provider;
use Spryker\Yves\Router\Plugin\RouteProvider\AbstractRouteProviderPlugin;
use Spryker\Yves\Router\Route\RouteCollection;
class HelloWorldRouteProviderPlugin extends AbstractRouteProviderPlugin
{
protected const ROUTE_HELLO_WORLD = 'hello-world';
/**
* @param \Spryker\Yves\Router\Route\RouteCollection $routeCollection
*
* @return \Spryker\Yves\Router\Route\RouteCollection
*/
public function addRoutes(RouteCollection $routeCollection): RouteCollection
{
$route = $this->buildRoute('/hello-world', 'HelloWorld', 'Index', 'indexAction');
$routeCollection->add(static::ROUTE_HELLO_WORLD, $route);
return $routeCollection;
}
}
- In the application, register
RouteProviderPlugin
, so the application knows about your controller action. - In the
Router
module, go toRouterDependencyProvider::getRouteProvider()
method and addHelloWorldRouteProviderPlugin
to the array. - Add the twig file to render your Hello World page.
- In
HelloWorld
, add theTheme/default/views/index
folder structure.
This folder structure reflects your theme and controller names. Default is the theme name, and index is the controller name. For every action, there is a template with the same name.
As your action is called index, add a twig file for your action called index.twig
:
{% extends template('page-layout-main') %}
{% define data = {
helloWorld: _view.helloWorld
} %}
{% block content %}
<div><h2>{{ data.helloWorld }}</h2></div>
{% endblock %}
- Open the new page
http://www.de.suite.local/hello-world
.
Thank you!
For submitting the form