<p>When you take a deeper look into <code>redirectToPostedUrl</code> you'll see <a href="https://github.com/craftcms/cms/blob/master/src/web/View.php#L484" rel="nofollow noreferrer">these lines</a>. All they do is convert the <code>Object</code> into an array, so they fetch every property of the object and pass those to the template to use them directly. You have everything the Object has.</p>

<p>You can access those attributes via short syntax <code>{title}</code> which is the same as <code>{{ object.title }}</code>. </p>

<p>For example if you have an <code>yii/model/MyFancyModel</code> with the following attributes</p>

<pre><code>[
    'id' =&gt; 4,
    'title' =&gt; 'foo',
    'someRelation =&gt; ['foo' =&gt; 'bar']
]
</code></pre>

<p>Craft will merge this array with <code>['object' =&gt; $object]</code> and you have access to <code>{ id }</code>, <code>{ title }</code>, <code>{ someRelation.foo }</code> as well as <code>{{ object.id }}</code> (and all others) as well as all other variables passed via <code>variables</code> the third attribute of the function, since <code>redirectToPostedUrl</code> doesn't pass a third argument these additional attributes are an empty array.</p>

<p>In your case <code>{ siteUrl }</code> is nothing else than <code>{{ object.siteUrl }}</code> which is basically <code>{{ entry.siteUrl }}</code> that means the <code>siteUrl</code> of your Element. When you do <code>{ type.handle }</code> (with an Entry)  it will render the <code>handle</code> of your <code>EntryType</code>. When your Object is a user you can do <code>{ email }</code> to display his/her email.</p>

<p>One more note: The attributes of the Model are fetched via <code>Arrayable</code> Trait if it has it. So it grabs all <code>fields</code> and <code>extraFields</code> gets those attributes. I can provide further explanations about <code>fields</code> and how they work exactly in detail if you like</p>

CC BY-SA 4.0
2019-02-19T05:44:33.447Z
Hey Robin. The thing is `$object->siteUrl`does not exist on the model I'm passing to `$this->redirectToPostedUrl($object);` I don't know where it's coming from. If I kill the script and try to access `$object->siteUrl` directly, I get a `yii\base\UnknownPropertyException`
Ben Parizek
CC BY-SA 4.0
2019-02-18T19:09:19.657Z
2019-02-18T19:16:58.417Z
2019-02-18T19:16:58.417Z
Robin Schambach
<p>Craft's Web Controller has a <a href="https://github.com/craftcms/cms/blob/3.1.12/src/web/Controller.php#L312" rel="nofollow noreferrer">redirectToPostedUrl</a> method. Copied here for the sake of discussion:</p>

<pre><code>public function redirectToPostedUrl($object = null, string $default = null): YiiResponse
{
    $url = Craft::$app-&gt;getRequest()-&gt;getValidatedBodyParam('redirect');
    if ($url === null) {
        if ($default !== null) {
            $url = $default;
        } else {
            $url = Craft::$app-&gt;getRequest()-&gt;getPathInfo();
        }
    }
    if ($object) {
        $url = Craft::$app-&gt;getView()-&gt;renderObjectTemplate($url, $object);
    }
    return $this-&gt;redirect($url);
}
</code></pre>

<p>We can call this method from our plugin controllers to redirect to the posted URL in the <code>redirect</code> input field and use a dynamic <code>{siteUrl}</code> variable. In my template, I have a redirect value that uses <code>{siteUrl}</code>:</p>

<pre><code>&lt;input type="hidden" name="redirect" value="6026760a62b9c6007a4b5695a65acbabG2540f8cb3727b998a1c78da45db863f{siteUrl}?id={id}"&gt;
</code></pre>

<p>And in my Controller, I receive that value and redirect to that location:</p>

<pre><code>return $this-&gt;redirectToPostedUrl($object);
</code></pre>

<p>That all works fine. I can pass my custom <code>$object</code> to this method and the <code>{id}</code> or any other part of the model I wish to process can appear in the URL.</p>

<p>My question is, where is <code>{siteUrl}</code> being made available to the <code>redirectToPostedUrl</code> method? In looking closer at <code>renderObjectTemplate</code> it never appears to specifically make global variables available. And, if I try other global variables such as <code>{currentUser.id}</code> nothing gets rendered. In Craft 2 I believe this method made <code>environmentalVariables</code> available for parsing but those are no longer supported in Craft 3.</p>

<p>So why and how does <code>{siteUrl}</code> work? Are there other variables that are available to be used in the redirect URLs too that we don't explicitly make available by passing them in via the <code>$object</code> argument?</p>

<plugin-development><templating>
What variables are available to redirectToPostedUrl?
# What variables are available to redirectToPostedUrl?

(Asked by: username_0 on 2019-02-17)

Craft's Web Controller has a redirectToPostedUrl method. Copied here for the sake of discussion:
public function redirectToPostedUrl($object = null, string $default = null): YiiResponse
{
    $url = Craft::$app->getRequest()->getValidatedBodyParam('redirect');
    if ($url === null) {
        if ($default !== null) {
            $url = $default;
        } else {
            $url = Craft::$app->getRequest()->getPathInfo();
        }
    }
    if ($object) {
        $url = Craft::$app->getView()->renderObjectTemplate($url, $object);
    }
    return $this->redirect($url);
}

We can call this method from our plugin controllers to redirect to the posted URL in the redirect input field and use a dynamic {siteUrl} variable. In my template, I have a redirect value that uses {siteUrl}:


And in my Controller, I receive that value and redirect to that location:
return $this->redirectToPostedUrl($object);

That all works fine. I can pass my custom $object to this method and the {id} or any other part of the model I wish to process can appear in the URL.
My question is, where is {siteUrl} being made available to the redirectToPostedUrl method? In looking closer at renderObjectTemplate it never appears to specifically make global variables available. And, if I try other global variables such as {currentUser.id} nothing gets rendered. In Craft 2 I believe this method made environmentalVariables available for parsing but those are no longer supported in Craft 3.
So why and how does {siteUrl} work? Are there other variables that are available to be used in the redirect URLs too that we don't explicitly make available by passing them in via the $object argument?

**Tags:** plugin-development, templating


---

## 1 answers

### Answer by username_1 on 2019-02-18. Score: 2

When you take a deeper look into redirectToPostedUrl you'll see these lines. All they do is convert the Object into an array, so they fetch every property of the object and pass those to the template to use them directly. You have everything the Object has.
You can access those attributes via short syntax {title} which is the same as {{ object.title }}. 
For example if you have an yii/model/MyFancyModel with the following attributes
[
    'id' => 4,
    'title' => 'foo',
    'someRelation => ['foo' => 'bar']
]

Craft will merge this array with ['object' => $object] and you have access to { id }, { title }, { someRelation.foo } as well as {{ object.id }} (and all others) as well as all other variables passed via variables the third attribute of the function, since redirectToPostedUrl doesn't pass a third argument these additional attributes are an empty array.
In your case { siteUrl } is nothing else than {{ object.siteUrl }} which is basically {{ entry.siteUrl }} that means the siteUrl of your Element. When you do { type.handle } (with an Entry)  it will render the handle of your EntryType. When your Object is a user you can do { email } to display his/her email.
One more note: The attributes of the Model are fetched via Arrayable Trait if it has it. So it grabs all fields and extraFields gets those attributes. I can provide further explanations about fields and how they work exactly in detail if you like

> **Comments:**
> **username_0**: Hey Robin. The thing is `$object->siteUrl`does not exist on the model I'm passing to `$this->redirectToPostedUrl($object);` I don't know where it's coming from. If I kill the script and try to access `$object->siteUrl` directly, I get a `yii\base\UnknownPropertyException`  

---

