Search code examples
concrete5concrete5-5.7

Concrete5 5.7: Passing variable from Controller to View


So I have made a dashboard Single Page under:

/application/single_pages/dashboard/newsletter.php

and a

/application/single_pages/dashboard/view.php

The Controller is under:

/Concrete/controllers/single_pages/dashboard/newsletter.php

The Controller looks like:

<?php
namespace Concrete\Controller\SinglePage\Dashboard;
use Concrete\Core\Multilingual\Page\PageList;
use \Concrete\Core\Page\Controller\DashboardPageController;
class Newsletter extends DashboardPageController {
    public function view() {
        $testVar = array(
            'one' => 'some',
            'two' => 'value',
            'three' => 'foo',
            'four' => 'bar'
        );
        $this->set('test', $testVar);
    }
}

The /application/single_pages/dashboard/newsletter.php looks like:

<?php defined('C5_EXECUTE') or die("Access Denied.");
echo 'something';
print_r($test);

The /application/single_pages/dashboard/view.php looks like:

<?php defined('C5_EXECUTE') or die("Access Denied.");

THE PROBLEM:

The local variable $test doesn't show up in the view.

The echo 'something'; is showing, so basically the page is up and running. What am I doing wrong?


Solution

  • It was a path & namespace issue:

    The path for the controller must be:

    /application/controllers/single_page/dashboard/newsletter.php
    

    instead of:

    /concrete/controllers/single_pages/dashboard/newsletter.php
    

    For the controller path its singular single_page but for the Single Page itself its plural single_pages


    When extending from the core, we need to adapt the namespace to:

    Application\Controller\SinglePage\Dashboard
    

    instead of:

    Concrete\Controller\SinglePage\Dashboard