How to load models in dashboard pages from package

Permalink
Hello,

I can't figure out how to load a model in my custom package from a dashboard controller (or in any other part of my package).

The folder structure of my package is:

packages
└── my_navigation_package
    ├── blocks
    │   └── my_navigation
    │       ├── add.php
    │       ├── bootstrap.php
    │       ├── controller.php
    │       ├── db.xml
    │       ├── edit.php
    │       ├── icon.png
    │       └── view.php
    ├── controller.php
    ├── controllers
    │   └── single_page
    │       └── dashboard


The namespace of my package is

Concrete\Package\MyNavigationPackage


Here is the code of my model (saved as models/my_test_model.php):
<?php
/**
 * Test Model
 */
namespace Concrete\Package\MyNavigationPackage\Models;
use Concrete\Core\Legacy\Model;
use Loader;
class MyTestModel extends Model
{
    // here comes the voodoo
}


And here it the code snipped of my dashboard controller (my_navigation_settings.php):
<?php
/**
 * Controller for the dashboard page
 */
namespace Concrete\Package\MyNavigationPackage\Controller\SinglePage\Dashboard\My;
use \Concrete\Core\Page\Controller\DashboardPageController;
use \Concrete\Package\MyNavigationPackage\Models\MyTestModel;
class MyNavigationSettings extends DashboardPageController
{
    function view()
    {
        // create instance of the test model
        $m = new MyTestModel();  
    }
}


The error that is thrown is "Class 'Concrete\Package\MyNavigationPackage\Models\MyTestModel' not found"

I already tried to change the namespaces but without luck.

Please can you explain what i'm doing wrong?

elpado
 
rge replied on at Permalink Reply
You don't necessary have to use the legacy way. If you follow the PSR-4 standard as mentioned in the documentation. All namespaces following this standard get auto loaded by Concrete.

I will give you an example that I posted in my own form question.

Custom class
path: packages/test/blocks/test/models/file.php
namespace Concrete\Package\Test\Block\Test\Models;
defined('C5_EXECUTE') or die (_('Access Denied'));
class File{
}


Block Controller
path: packages/test/blocks/test/models/file.php

namespace Concrete\Package\Test\Block\Test;
use Concrete\Core\BLock\BlockController;
use Concrete\Package\Test\Block\Test\Models\Test; //use the custom clas
use Core;
defined('C5_EXECUTE') or die(_("Access Denied."));
class Controller extends BlockController
{
public function view()
   {
      $test = new Test();
   }
}


Link to post:http://www.concrete5.org/community/forums/customizing_c5/package-lo...

Hope this helps