Package controller location with Loader::controller

Permalink
Hello,
I'm building a package with a couple of tools files that need to access controllers functions. I'm able to reach a controller in
/concrete/controllers/dashboard/users, when creating a instance of the controller like:

$cnt = Loader::controller('/dashboard/users/test');
$getData = $cnt->view();


But when using
$cnt = Loader::controller('/packages/package_name/controllers/dashboard/controller');
$getData = $cnt->view();


Controller file:
<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
class PackageNameControllersDashboardController extends Controller {
   public function view() {
      echo "test";
   }
}
?>

GreyhorseDesign
 
JohntheFish replied on at Permalink Reply
JohntheFish
In your second example, the loader needs the package handle as a second parameter:
$cnt = Loader::controller('controllers/dashboard/controller', 'package_handle');


You also need to tidy up naming, so:
class DashboardMyPackageMyPageController extends Controller {


in file
/packages/my_package/controllers/dashboard/my_package/my_page.php


and loaded as
Loader::controller('dashboard/my_package/my_page', 'my_package');
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
Thank you for you reply. I used the package handle defined in the package controller like

protected $pkgHandle = 'package_handle';


I also did everything you suggested with naming the controller file class and folder structure

Still I get:
Fatal error: Call to undefined method Controller::view() in...
JohntheFish replied on at Permalink Reply
JohntheFish
I think copying and pasting from your original post led me to making a mistake. Try:
Loader::controller('dashboard/my_package/my_page', 'package_handle');
$cnt = new DashboardMyPackageMyPageController();

File:
packages/my_package/controllers/dashboard/my_package/my_page.php
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
Thank you for you answer but that doesn't seem to work either.

I've allready changed

Loader::controller('controllers/dashboard/controller', 'package_handle');


to:

Loader::controller('controllers/dashboard/myPackage/controller', 'package_handle');
JohntheFish replied on at Permalink Reply
JohntheFish
I suspect there is a misunderstanding in the translation between abstract examples and the actual filename, class and loader instruction.

Rather than abstract generalisations, please list:
- The complete file path and name
- The actual package handle
- The actual class declaration inside the file
- The actual loader call

PS. Just noticed another typo in my 7:48 post. Have corrected it above.
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
I abstracted the whole thing so hopefully (when you could provide the correct namings/loader call) it could serve as a good guideline for people facing the same problem

The complete file path and name:
mywebsite.com/public_html/packages/my_package_name/controllers/dashboard/my_package_name/controller.php

---

The actual package handle:
my_package_handle

---

The actual class declaration inside the file:
class DashboardMyPackageNameControllerController Extends Controller

---

The actual loader call:
Loader::controller('dashboard/my_package_name/controller', 'my_package_handle');
$cnt = new DashboardMyPackageNameControllerController();

---

Tring to perform a function:
//$data = $cnt->FunctionToGetData();
JohntheFish replied on at Permalink Reply
JohntheFish
In
mywebsite.com/public_html/packages/my_package_name/controllers/dashboard/my_package_name/controller.php

the part 'dashboard/my_package_name' creates a region in the dashboard and, as long as there is a matching single page in the package, '/dashboard/my_package_name/controller.php' will show as a dashboard page in that region.

In the package controller you would need to install the single page for it to work as a single page. If not, you should still be able to load the controller.

So, with the above in mind, can you see the single page in the dashboard and navigate to it?

Have a look at a simple dashboard package like my 'config info' dashboard addon for a working example. It installs in an existing dashboard region rather than creating its own, but the overall structuring principle is the same.