Implementing URL routing in Yves

Edit on GitHub

The 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 this request, follow these steps:

  1. Create a plugin that extends AbstractRouteProviderPlugin in the module where the controller is defined, under the Plugin/Router folder. AbstractRouteProviderPlugin enables 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
  1. In the new created plugin, which extends AbstractRouteProviderPlugin, implement the route in the addRoutes(RouteCollection $routeCollection): RouteCollection operation:
<?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;
    }
}
  1. Activate the plugin in Pyz\Yves\Route\RouterDependencyProvider::getRouteProvider(): array:
protected function getRouteProvider(): array
{
    return [
        //...
        new HelloRouteProvider($isSsl),
    ];
}
  1. 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.

  1. To add a route with parameters, 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;
    }
}
  1. Use this parameter in the controller:
public function helloAction(Request $request)
{
    $name = $request->attributes->get('name');
}
  1. Open http://mysprykershop.com/hello/spryker to make a request using the newly configured route with the name parameter having its value set to spryker.