Sage: Controller is loaded twice

Created on 8 Nov 2018  路  4Comments  路  Source: roots/sage

Submit a feature request or bug report


I installed master branch.
I modified FrontPage controller to :

<?php
namespace App\Controllers;

use Sober\Controller\Controller;

class FrontPage extends Controller
{
    public function __construct(){
        echo 'test';
    }
}

(This code is only to see the problem, it is not "production code")
What is the current behavior?

On the frontpage, I see "testtest", Which means that FrontPage is instantiate twice.

What is the expected or desired behavior?

On the frontpage, I should see "test". Which means that FrontPage is instantiate only once.


Bug report

Please provide steps to reproduce, including full log output:
Just install sage with command composer create-project roots/sage sage-master dev-master
Switch to this theme.
Maybe you should set a static page as homepage in WP settings.
Go to website url root.

I looked backtrace and a first loaded is due to soberwp/Controller filters : https://github.com/soberwp/controller/blob/master/controller.php#L107
and a second load is due to this apply_filters : https://github.com/roots/sage/blob/master/app/filters.php#L51

Most helpful comment

You shouldn't echo from __construct

If you're trying to return a value, use a public function and return it rather than echo.

All 4 comments

You shouldn't echo from __construct

If you're trying to return a value, use a public function and return it rather than echo.

Yes, of course. It is only an example to easily see and reproduce the problem : FrontPage object is instantiated twice. It is a performance issue.

@ajoah can you create an issue on https://github.com/soberwp/controller and I will take a look at it.

@darrenjacoby It's done. I hesitated to open an issue here or on soberwp/controller because code of sage is involved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emilsgulbis picture emilsgulbis  路  6Comments

Geczy picture Geczy  路  5Comments

jlariza picture jlariza  路  5Comments

retlehs picture retlehs  路  3Comments

nicooprat picture nicooprat  路  5Comments