Implement URL routing in Yves
Edit on GitHubThe words contained in an URL play a major factor for a search engine to determine if the page is relevant for a specific search query. URL routing is a mechanism used to map URLs to the code that gets executed when a specific request is being submitted. URL routing makes URLs more human-readable and SEO-friendly.
To implement URL Routing in Yves, follow these steps per scenario:
Scenario 1: You need to route requests made on URL /hello to the action helloAction(Request $request) implemented in the DemoController.
To route the preceding request, follow these steps:
- Create a plugin that extends AbstractRouteProviderPluginin the module where the controller is defined, under thePlugin/Routerfolder.AbstractRouteProviderPluginenables setting up the HTTP method used (GET or POST) when setting up the new route.
<?php
protected function buildGetRoute(string $path, string $moduleName, string $controllerName, string $actionName = 'indexAction'): Route
protected function buildPostRoute(string $path, string $moduleName, string $controllerName, string $actionName = 'indexAction'): Route
protected function buildRoute(string $path, string $moduleName, string $controllerName, string $actionName = 'indexAction'): Route
- In the new created plugin, which extends AbstractRouteProviderPlugin, implement the route in theaddRoutes(RouteCollection $routeCollection): RouteCollectionoperation:
<?php
use Spryker\Yves\Router\Plugin\RouteProvider\AbstractRouteProviderPlugin;
class HelloRouteProviderPlugin extends AbstractRouteProviderPlugin
{
    private const ROUTE_HELLO = 'hello';
    public function addRoutes(RouteCollection $routeCollection): RouteCollection
    {
        $routeCollection = $this->addHelloRoute($routeCollection);
        return $routeCollection;
    }
    protected function addHelloRoute(RouteCollection $routeCollection): RouteCollection
    {
        $route = $this->buildRoute('/hello', 'Demo', 'Demo', 'helloAction');
        $routeCollection->add(static::ROUTE_HELLO, $route);
        return $routeCollection;
    }
}
- Activate the plugin in Pyz\Yves\Route\RouterDependencyProvider::getRouteProvider(): array:
protected function getRouteProvider(): array
{
    return [
        //...
        new HelloRouteProvider($isSsl),
    ];
}
- To make a request using the newly configured route in your browser, open http://mysprykershop.com/hello
Scenario 2: You need to route requests made on URL /hello/{name} to the action helloAction(Request $request) implemented in DemoController, which generates different content based on the value of the name parameter.
To add a route with parameters, follow these steps:
- You can use curly braces syntax:
<?php
use Spryker\Yves\Router\Plugin\RouteProvider\AbstractRouteProviderPlugin;
class HelloRouteProvider extends AbstractRouteProviderPlugin
{
    private const ROUTE_HELLO = 'hello';
    public function addRoutes(RouteCollection $routeCollection): RouteCollection
    {
        $routeCollection = $this->addHelloRoute($routeCollection);
        return $routeCollection;
    }
    protected function addHelloRoute(RouteCollection $routeCollection): RouteCollection
    {
        $route = $this->buildRoute('/hello/{name}', 'Demo', 'Demo', 'helloAction');
        $routeCollection->add(static::ROUTE_HELLO, $route);
        return $routeCollection;
    }
}
- Use this parameter in the controller:
public function helloAction(Request $request)
{
    $name = $request->attributes->get('name');
}
- Open http://mysprykershop.com/hello/sprykerto make a request using the newly configured route with thenameparameter having its value set tospryker.
Thank you!
For submitting the form