Overview

Namespaces

  • Faulancer
    • Controller
    • Exception
    • Form
      • Validator
        • Type
    • Helper
      • Reflection
    • Http
    • Mail
    • ORM
      • User
    • Service
      • Factory
    • ServiceLocator
    • Session
    • View
      • Helper

Classes

  • AssetList
  • Block
  • CodeBlock
  • EndBlock
  • EscapeString
  • FlashBag
  • FormData
  • FormError
  • GenerateCsrfToken
  • Highlighter
  • ParentTemplate
  • RenderBlock
  • RenderView
  • Route
  • Translate
  • User
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 
<?php
/**
 * Class Route | Route.php
 * @package Faulancer\View\Helper
 * @author  Florian Knapp <office@florianknapp.de>
 */
namespace Faulancer\View\Helper;

use Faulancer\Exception\RouteInvalidException;
use Faulancer\Service\Config;
use Faulancer\ServiceLocator\ServiceLocator;
use Faulancer\View\AbstractViewHelper;
use Faulancer\View\ViewController;

/**
 * Class Route
 */
class Route extends AbstractViewHelper
{

    /**
     * Get route path by name
     *
     * @param ViewController $view
     * @param string         $name
     * @param array          $parameters
     * @return string
     * @throws RouteInvalidException
     */
    public function __invoke(ViewController $view, string $name, array $parameters = [])
    {
        /** @var Config $config */
        $config = ServiceLocator::instance()->get(Config::class);
        $routes = $config->get('routes');
        $path   = '';

        foreach ($routes as $routeName => $data) {

            if ($routeName === $name) {
                $path = preg_replace('|/\((.*)\)|', '', $data['path']);
                break;
            }

        }

        if (empty($path)) {
            throw new RouteInvalidException('No route for name "' . $name . '" found');
        }

        if (!empty($parameters)) {
            $path = $path . '/' . implode('/', $parameters);
        }

        return $path;
    }

}
API documentation generated by ApiGen