Yii2 use runAction() within beforeAction method
<p>I have a MainController which extends Controller. All my app's controllers extend from MainController which includes various methods and properties which need to be accessible from any Controller.</p>

<p>Within my MainController is <code>beforeAction</code>, which does several things:</p>

<ul>
<li>Checks for redirects held in the database and performs them if the URL matches one in the DB.</li>
<li>Generates <code>&lt;head&gt;</code> data for each controller</li>
<li>Gets the language and country the user is looking at based on cookie and slug of the URL. (i.e. <a href="http://example.com/netherlands" rel="nofollow">http://example.com/netherlands</a>).</li>
<li>Will render a generic page from a template if URL matches one from the database's <code>pages</code> table.</li>
</ul>

<p>It's the last that I am struggling with. In my MainController I have this:</p>

<pre><code>/**
* Before action, check all $this-&gt;before_actions satisfy. If no head_data provided, try and fill in some basics
*/
public function beforeAction( $action )
{

    // Run parent method first
    if (!parent::beforeAction($action))
        return false;

    // Check redirects
    $this-&gt;checkRedirects();

    if( $this-&gt;checkPages() )
    {

        // If not error page, loop through before methods
        if( $action-&gt;id !== 'error' )
        {

            // Loop through actions to peform and do them
            foreach ( $this-&gt;before_actions as $before_method )
                $this-&gt;$before_method();
        }
        return true;
    }

}
</code></pre>

<p>Where $this->checkPages() contains the following:</p>

<pre><code>/**
* Check for pages
*/
public function checkPages()
{

    // Attempt to find page for this request
    $page = Page::find()-&gt;where( [ 'permalink' =&gt; trim( str_replace( getBaseUrl() , "", getCurrentUrl() ), "/" ) ] )-&gt;one();

    // If found, load it instead
    if( !empty( $page ) )
        return Yii::$app-&gt;runAction( "pages/show", [ 'id' =&gt; $page-&gt;id ] );

    // Else, return
    return true;

}
</code></pre>

<p>The issue I am having is that if I go to <a href="http://example.com/story" rel="nofollow">http://example.com/story</a>, because there is no StoryController, the returns a 404 error although the action does run and the view "views/story/show" is output.</p>

<p>How can I prevent this?</p>

<p>EDIT:</p>

<p>To add, the log shows that it first says:
<code>"Unable to resolve the request 'story/index'"</code>.</p>

<p>But then additional logs show:
<code>"Route to run: pages/show"</code> ... <code>"Running action: app\controllers\PagesController::actionShow()"</code>
..
<code>Rendering view file: /Users/stefandunn/Documents/Local Machine Development/views/pages/show.php</code></p>

<p>So I am guessing it's the first log result causing the 404 status</p>