0

Final Class Yiisoft\Yii\AuthClient\Widget\AuthChoice

InheritanceYiisoft\Yii\AuthClient\Widget\AuthChoice » Yiisoft\Widget\Widget

AuthChoice renders buttons for authentication via various OAuth2 clients.

By default, it renders client links that redirect to the auth route; with {@see \Yiisoft\Yii\AuthClient\Widget\popupMode()} enabled, it opens a popup window for the OAuth2 flow. The widget retrieves clients from the injected {@see \Yiisoft\Yii\AuthClient\Collection} instance.

Example:

<?= AuthChoice::widget()->authRoute('site/auth'); ?>

Customize appearance with {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\end()} and {@see \Yiisoft\Yii\AuthClient\Widget\clientLink()} or {@see \Yiisoft\Yii\AuthClient\Widget\createClientUrl()}:

<?php
use Yiisoft\Yii\AuthClient\Widget\AuthChoice;

$authChoice = AuthChoice::widget()->authRoute('site/auth');
$authChoice->begin();
?>
<ul>
<?php foreach ($authChoice->getClients() as $client): ?>
    <li><?= $authChoice->clientLink($client) ?></li>
<?php endforeach; ?>
</ul>
<?= AuthChoice::end() ?>

Configuration methods ({@see \Yiisoft\Yii\AuthClient\Widget\authRoute()}, {@see \Yiisoft\Yii\AuthClient\Widget\popupMode()}, {@see \Yiisoft\Yii\AuthClient\Widget\displayMode()}, {@see \Yiisoft\Yii\AuthClient\Widget\iconAttributes()}, {@see \Yiisoft\Yii\AuthClient\Widget\iconWrapperAttributes()}, {@see \Yiisoft\Yii\AuthClient\Widget\iconWidth()}, {@see \Yiisoft\Yii\AuthClient\Widget\iconHeight()}, {@see \Yiisoft\Yii\AuthClient\Widget\linkAttributes()}, {@see \Yiisoft\Yii\AuthClient\Widget\options()}, {@see \Yiisoft\Yii\AuthClient\Widget\clientOptions()}) must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()}, as asset registration and the opening <div> tag are produced during rendering.

Inline SVG icons are rendered via {@see \Yiisoft\Yii\AuthClient\Widget\renderClientLogo()}, always wrapped in a <span class="auth-icon {name}"> styling/testing hook (customizable via {@see \Yiisoft\Yii\AuthClient\Widget\iconWrapperAttributes()}). A client-provided logo (set via {@see \Yiisoft\Yii\AuthClient\OAuth2Interface::setLogo()}) is embedded verbatim inside it, exactly as given. Otherwise the logo registry is used, falling back to an empty span if the client is unregistered. Icon sizing/styling via {@see \Yiisoft\Yii\AuthClient\Widget\iconWidth()}, {@see \Yiisoft\Yii\AuthClient\Widget\iconHeight()}, and {@see \Yiisoft\Yii\AuthClient\Widget\iconAttributes()} applies to the inner <svg> of registry icons only, not client-provided logos. Display mode ({@see \Yiisoft\Yii\AuthClient\Widget\AuthChoiceDisplayMode::Icon}, {@see \Yiisoft\Yii\AuthClient\Widget\AuthChoiceDisplayMode::Text}, or {@see \Yiisoft\Yii\AuthClient\Widget\AuthChoiceDisplayMode::Both}) is set via {@see \Yiisoft\Yii\AuthClient\Widget\displayMode()}.

This widget respects the following keys from {@see \Yiisoft\Yii\AuthClient\AuthClientInterface::getViewOptions()}:

  • popupWidth: int, width of the popup window in pixels (popup mode only).
  • popupHeight: int, height of the popup window in pixels (popup mode only).
  • widget: array, configuration for rendering a client link via a custom {@see \Yiisoft\Yii\AuthClient\Widget\AuthChoiceItem} subclass instead of the default markup.

See also Yiisoft\Yii\AuthClient\AuthAction.

Protected Methods

Hide inherited methods

Method Description Defined By
renderMainContent() Renders the main content, which includes all external services links. Yiisoft\Yii\AuthClient\Widget\AuthChoice

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Yii\AuthClient\Collection $clientCollection, \Yiisoft\Router\UrlGeneratorInterface $urlGenerator, \Yiisoft\Assets\AssetManager $assetManager )
$clientCollection Yiisoft\Yii\AuthClient\Collection
$urlGenerator \Yiisoft\Router\UrlGeneratorInterface
$assetManager \Yiisoft\Assets\AssetManager

                public function __construct(
    Collection $clientCollection,
    private readonly UrlGeneratorInterface $urlGenerator,
    private readonly AssetManager $assetManager,
) {
    $this->clients = $clientCollection->getClients();
}

            
authRoute() public method

public self authRoute ( string $authRoute )
$authRoute string

                public function authRoute(string $authRoute): self
{
    $this->authRoute = $authRoute;
    return $this;
}

            
begin() public method

Opens the widget: registers assets and echoes the opening <div> tag, so that content written directly to output between {@see begin()} and {@see end()} appears nested inside it.

public ?string begin ( )

                public function begin(): ?string
{
    parent::begin();
    echo $this->renderOpenTag();
    return null;
}

            
clientLink() public method

Outputs client auth link.

public string clientLink ( Yiisoft\Yii\AuthClient\OAuth2Interface $client, string $text null, array $htmlOptions = [] )
$client Yiisoft\Yii\AuthClient\OAuth2Interface

Extending from an auth client instance.

$text string

Link text, if not set - default value will be generated.

$htmlOptions array

Link HTML options.

return string

Generated HTML.

throws Yiisoft\Yii\AuthClient\Exception\InvalidConfigException

on wrong configuration.

throws \Yiisoft\Definitions\Exception\InvalidConfigException

clientOptions() public method

public self clientOptions ( array $clientOptions )
$clientOptions array

Additional options passed to the underlying JS plugin. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function clientOptions(array $clientOptions): self
{
    $this->clientOptions = $clientOptions;
    return $this;
}

            
createClientUrl() public method

Composes client auth URL.

public string createClientUrl ( Yiisoft\Yii\AuthClient\AuthClientInterface $client )
$client Yiisoft\Yii\AuthClient\AuthClientInterface

External auth client instance.

return string

Auth URL.

                public function createClientUrl($client): string
{
    /** @infection-ignore-all Disable auto-render when URL is requested directly; caller handles rendering */
    $this->autoRender = false;
    $params = [];
    $params[$this->clientIdGetParamName] = $client->getName();
    return $this->urlGenerator->generate($this->authRoute, $params);
}

            
displayMode() public method

public self displayMode ( \Yiisoft\Yii\AuthClient\Widget\AuthChoiceDisplayMode $displayMode )
$displayMode \Yiisoft\Yii\AuthClient\Widget\AuthChoiceDisplayMode

                public function displayMode(AuthChoiceDisplayMode $displayMode): self
{
    $this->displayMode = $displayMode;
    return $this;
}

            
getClient() public method

public Yiisoft\Yii\AuthClient\OAuth2Interface getClient ( string $name )
$name string

                public function getClient(string $name): OAuth2Interface
{
    $clients = array_filter(
        $this->getClients(),
        fn($client) => $client->getName() === $name,
    );
    $client = end($clients);
    if ($client === false) {
        throw new InvalidConfigException("OAuth2 client with name '{$name}' not found.");
    }
    return $client;
}

            
getClients() public method

public array getClients ( )

                public function getClients(): array
{
    return $this->clients;
}

            
iconAttributes() public method

public self iconAttributes ( array $iconAttributes )
$iconAttributes array

HTML attributes for the inner <svg> of registry icons (e.g. sizing utility classes, fill, ARIA attributes). Not applied to client-provided logos or the empty placeholder - see {@see \Yiisoft\Yii\AuthClient\Widget\iconWrapperAttributes()} for attributes that apply to every icon. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function iconAttributes(array $iconAttributes): self
{
    $this->iconAttributes = $iconAttributes;
    return $this;
}

            
iconHeight() public method

public self iconHeight ( string|null $iconHeight )
$iconHeight string|null

Height of SVG icons (e.g., '24'). Set to null to omit the attribute. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function iconHeight(?string $iconHeight): self
{
    $this->iconHeight = $iconHeight;
    return $this;
}

            
iconWidth() public method

public self iconWidth ( string|null $iconWidth )
$iconWidth string|null

Width of SVG icons (e.g., '24'). Set to null to omit the attribute. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function iconWidth(?string $iconWidth): self
{
    $this->iconWidth = $iconWidth;
    return $this;
}

            
iconWrapperAttributes() public method

public self iconWrapperAttributes ( array $iconWrapperAttributes )
$iconWrapperAttributes array

HTML attributes for the icon's wrapping <span>, merged with the default ['class' => 'auth-icon {name}']. Unlike {@see \Yiisoft\Yii\AuthClient\Widget\iconAttributes()} (which only affects the inner <svg> of registry icons), these apply to every icon regardless of source - registry, custom logo, or the empty placeholder. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function iconWrapperAttributes(array $iconWrapperAttributes): self
{
    $this->iconWrapperAttributes = $iconWrapperAttributes;
    return $this;
}

            
linkAttributes() public method

public self linkAttributes ( array $linkAttributes )
$linkAttributes array

HTML attributes for auth links, merged with default ['class' => 'auth-link']. Use this to apply framework-specific classes (e.g., Bootstrap's 'btn btn-outline-secondary'). Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function linkAttributes(array $linkAttributes): self
{
    $this->linkAttributes = $linkAttributes;
    return $this;
}

            
options() public method

public self options ( array $options )
$options array

The HTML attributes for the container <div> tag, see {@see \Yiisoft\Html\Html::renderTagAttributes()}. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function options(array $options): self
{
    $this->options = $options;
    return $this;
}

            
popupMode() public method

public self popupMode ( boolean $popupMode )
$popupMode boolean

Whether a popup window should be used instead of direct links. Must be called before {@see \Yiisoft\Yii\AuthClient\Widget\begin()}/{@see \Yiisoft\Yii\AuthClient\Widget\render()} to take effect.

                public function popupMode(bool $popupMode): self
{
    $this->popupMode = $popupMode;
    return $this;
}

            
render() public method

Runs the widget.

public string render ( )
return string

Rendered HTML.

throws \Yiisoft\Definitions\Exception\InvalidConfigException

                public function render(): string
{
    $content = $this->renderOpenTag();
    if ($this->autoRender) {
        $content .= $this->renderMainContent();
    }
    $content .= Html::div()->close();
    return $content;
}

            
renderMainContent() protected method

Renders the main content, which includes all external services links.

protected string renderMainContent ( )
return string

Generated HTML.

throws Yiisoft\Yii\AuthClient\Exception\InvalidConfigException
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                protected function renderMainContent(): string
{
    $content = '';
    /**
     * @var OAuth2Interface $externalService
     */
    foreach ($this->getClients() as $externalService) {
        // clientLink() already returns rendered, safe-to-embed HTML.
        /** @infection-ignore-all Must concatenate to render all clients, not just the last one */
        $content .= $this->clientLink($externalService);
    }
    return $content;
}

            
setClients() public method

public void setClients ( array $clients )
$clients array

                public function setClients(array $clients): void
{
    $this->clients = $clients;
}