Single page controller doesn't seem to be loading

Permalink 2 users found helpful
I am working on a site and there is a dashboard single page that doesn't seem to be loading the controller. The single page HTML is output, but there are PHP errors that are displayed because the values are not being set from the controller.

To debug, on the first line of the single_page, I placed this code:
$abc = new DashboardMySinglePageController();


The result was:
Fatal error: Class 'DashboardMySinglePageController' not found in /var/www/packages/package_name/single_pages/dashboard/my_single_page.php on line 13


Any ideas why this is happening and/or how to fix it?

SkyBlueSofa
 
mkly replied on at Permalink Reply
mkly
99% of the time this happens to me is because I mis-typed the spelling or forgot to include the file structure.

If you have a single page called "Monkey Pants" you should name the file
monkey_pants.php

It should be in the root directory(unless its a package) like this
/controllers/dashboard/monkey_pants.php

the single page should be at
/single_pages/dashboard/monkey_pants.php

the monkey_pants.php controller should be
class DashboardMonkeyPantsController extends Controller {
  public function view() {
    $this->set('test', 'works');
  }
}

the monkey_pants.php view should be
<?php echo $test ?>


I don't believe that the controller will be in scope in the view. I think that kind of defeats the point of the abstraction. If you wanted to pass the controller to the view you can do.
// in controller
$this->set('controller', $this);
SkyBlueSofa replied on at Permalink Reply
SkyBlueSofa
On the local box, all works great. It broke when moving to the test box.

I have a single page at
/packages/primates/single_pages/dashboard/monkey_pants.php


The controller is at
/packages/primates/controllers/dashboard/monkey_pants/controller.php


The code in the controller is
class DashboardMonkeyPantsController extends Controller {
 public function view() {
   $this->set('test', array(1,2,3));
 }
}


But in the monkey_pants.php view doesn't work...it returns an error that $test doesn't exist.
<?php
foreach ($test as $t) {
echo $t;
}
 ?>


FYI: I'm not trying to get any weird info from the controller to the view, I was just trying to debug.
mkly replied on at Permalink Reply
mkly
Ya, I kind of thought you were just testing it. I think you might need to do
/packages/primates/single_pages/dashboard/monkey_pants/view.php

To get it to wire up with
/packages/primates/controllers/dashboard/monkey_pants/controller.php

Not
/packages/primates/single_pages/dashboard/monkey_pants.php

Not positive as I haven't ever done it they way you have it, but my be worth a shot.
mkly replied on at Permalink Reply
mkly
Also, make sure you disable and clear your cache after each of these tests. I've found my cache gets stuck even if caching is off when I'm moving around single pages like this.
SkyBlueSofa replied on at Permalink Reply
SkyBlueSofa
After some more troubleshooting, I am still unable to find the issue, but may be getting closer.

I've been playing with the {root}/concrete/libraries/loader.php file. FYI, it first looks in the package directory for the monkey_pants.php file and then the monkey_pants/controller.php file. So your solution is just a bit slower.

On line 416, I put the code
print "|".DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $controllerFile."|";die();
to output the controller file that it is looking for:
/var/www/packages/primates/controllers/dashboard/monkey_pants.php

To my eyes, I see no issue, but the php function file_exists() run on that path returns false.

I've tried creating the /dashboard/monkey_pants.php at both the {root}/controllers level and the package level, but neither one works.

Blerg. Any other ideas?
mkly replied on at Permalink Reply
mkly
Beyond that I don't know. If file_exists doesn't find the file then I'm guessing there is something with the name or permissions? No idea past that.

And the reason for the /view.php and /controller.php in the directory is not because of speed but in order to get the Dashboard menus to display nicely.

If you look at the core dashboard pages you'll see that these redirect to another page in controller.php
SkyBlueSofa replied on at Permalink Reply
SkyBlueSofa
Permissions. D*** permissions! :D

I reset permissions on that particular file and it started working.

Thanks so much
mkly replied on at Permalink Reply
mkly
I should probably add this, depending on what you are trying to do as I kind of think you know what I just told you before.

Like I said above, this kind of defeats the point of MVC/MVP. If you need something like that you might want to look into a presenter/view model type class in you controller file
class DashboardMonkeyPantsController extends Controller() {
  public function view() {
    $view_model = new MonkeyPantsViewModel();
    $view_model->some_value = 5;
    $view_model->another_value = 8;
    $this->set('monkey_pants', $view_model);
  }    
}
class MonkeyPantsViewModel {
  public $some_value;
  public $another_value;
  public function some_computed_value() {
    return $some_value + $another_value;
  }
}


Then in your view you can use it like this in your view
<?php echo $monkey_pants->some_value ?>
<?php echo $monkey_pants->some_computed_value() ?>
bracketfire replied on at Permalink Reply
bracketfire
If you've been fooling with your code, don't forget about Dashboard -> Single Pages -> Refresh. It fixed the cFilename column in the Pages table and I was off and running.
jkoudys replied on at Permalink Reply
Came across this thread while trying to debug the same problem. Here's a quick summary so anyone happening across this doesn't need to read the whole thing:

- It's not really important if you say mypage.php or mypage/controller.php in the controller directory (or similar in the view), BUT, if you do try and config around this, make sure you do the 'refresh' in the single-pages entry in the dashboard
- Make sure that the paths match, so for single pages:
-- controllers/foo/bar.php
-- single_pages/foo/bar.php
and for page_types:
-- controllers/page_types/foo.php
-- themes/THEME_PATH/foo.php
- The class name is special - don't expect it to just know that the class name you created is correct. For a single_page foo/bar/baz, you must define a FooBarBazController class. For a page type, it would be FooBarBazPageTypeController. Both extend Controller. My mistake was just a typo in the class name.