0 follower

Final Class Yiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator

InheritanceYiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator

URL creator for action columns in GridView.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Creates a new URL creator instance. Yiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator
__invoke() Generates a URL for an action button. Yiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator

Method Details

Hide inherited methods

__construct() public method

Creates a new URL creator instance.

public __construct( \Yiisoft\Router\UrlGeneratorInterface $urlGenerator, \Yiisoft\Router\CurrentRoute $currentRoute, string $defaultPrimaryKey 'id', \Yiisoft\Yii\DataView\Url\UrlParameterType $defaultPrimaryKeyParameterType UrlParameterType::Query ): mixed
$urlGenerator \Yiisoft\Router\UrlGeneratorInterface

The URL generator service.

$currentRoute \Yiisoft\Router\CurrentRoute

The current route service.

$defaultPrimaryKey string

The default primary key field name. Used when not specified in the URL config.

$defaultPrimaryKeyParameterType \Yiisoft\Yii\DataView\Url\UrlParameterType

The default parameter type for primary key values. Used when not specified in the URL config.

                public function __construct(
    private readonly UrlGeneratorInterface $urlGenerator,
    private readonly CurrentRoute $currentRoute,
    private readonly string $defaultPrimaryKey = 'id',
    private readonly UrlParameterType $defaultPrimaryKeyParameterType = UrlParameterType::Query,
) {}

            
__invoke() public method

Generates a URL for an action button.

public __invoke( string $action, Yiisoft\Yii\DataView\GridView\Column\Base\DataContext $context ): string
$action string

The action name (e.g., 'view', 'edit', 'delete').

$context Yiisoft\Yii\DataView\GridView\Column\Base\DataContext

The data context containing the row data and column.

return string

The generated URL for the action.

throws LogicException

if the URL config is not an instance of ActionColumnUrlConfig.

                public function __invoke(string $action, DataContext $context): string
{
    /** @var ActionColumn $column */
    $column = $context->column;
    $config = $column->urlConfig ?? new ActionColumnUrlConfig();
    if (!$config instanceof ActionColumnUrlConfig) {
        throw new LogicException(self::class . ' supports ' . ActionColumnUrlConfig::class . ' only.');
    }
    $primaryKey = $config->primaryKey ?? $this->defaultPrimaryKey;
    $primaryKeyParameterType = $config->primaryKeyParameterType ?? $this->defaultPrimaryKeyParameterType;
    $primaryKeyValue = is_object($context->data)
        ? $context->data->$primaryKey
        : $context->data[$primaryKey];
    /** @psalm-suppress PossiblyNullOperand Assume that the current route matches. */
    $route = ($config->baseRouteName ?? $this->currentRoute->getName()) . '/' . $action;
    $arguments = $config->arguments;
    $queryParameters = $config->queryParameters;
    switch ($primaryKeyParameterType) {
        case UrlParameterType::Path:
            $arguments = array_merge($arguments, [$primaryKey => (string) $primaryKeyValue]);
            break;
        case UrlParameterType::Query:
            $queryParameters = array_merge($queryParameters, [$primaryKey => (string) $primaryKeyValue]);
            break;
    }
    return $this->urlGenerator->generate($route, $arguments, $queryParameters);
}