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.
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
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.
Most helpful comment
You shouldn't echo from
__constructIf you're trying to return a value, use a
public functionandreturnit rather thanecho.