Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Model cannot be found

Hello community

I'm new to Phalcon and I'm trying to do a simple thing which is getting the settings from the databse using the models.

This is my main controller:

<?php

use \settings\Settings;

class UserEndController extends \Phalcon\Mvc\Controller
{

    public function homeAction()
    {
        $settings = Settings::findFirst(1);

    }

}

But I encounter this error:

Fatal error: Uncaught Error: Class 'settings\Settings' not found in C:\xampp\htdocs\icriticize\app\controllers\UserEndController.php:11 
Stack trace: 
#0 [internal function]: UserEndController->homeAction() 
#1 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(UserEndController), 'homeAction', Array) 
#2 [internal function]: Phalcon\Dispatcher->dispatch() 
#3 C:\xampp\htdocs\icriticize\public\index.php(42): Phalcon\Mvc\Application->handle() 
#4 C:\xampp\htdocs\icriticize\.htrouter.php(30): require_once('C:\\xampp\\htdocs...') 
#5 {main} thrown in C:\xampp\htdocs\icriticize\app\controllers\UserEndController.php on line 11

BTW I run the app using phalcon serve. I'm on windows and using XAMPP with PHP x64

Your model class is not loaded into memory (require) . Check your Phalcon loader setup.



282
edited Jun '19

Thanks for you answer This is my loader.php file:

<?php

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir
    ]
)->register();

and this is my config file:

<?php
/*
 * Modified: prepend directory path of current file, because of this file own different ENV under between Apache and command line.
 * NOTE: please remove this comment.
 */
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');

return new \Phalcon\Config([
    'database' => [
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '',
        'dbname'      => 'icriticize',
        'charset'     => 'utf8',
    ],
    'application' => [
        'appDir'         => APP_PATH . '/',
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir'      => APP_PATH . '/models/',
        'migrationsDir'  => APP_PATH . '/migrations/',
        'viewsDir'       => APP_PATH . '/views/',
        'pluginsDir'     => APP_PATH . '/plugins/',
        'libraryDir'     => APP_PATH . '/library/',
        'cacheDir'       => BASE_PATH . '/cache/',

        // This allows the baseUri to be understand project paths that are not in the root directory
        // of the webpspace.  This will break if the public/index.php entry point is moved or
        // possibly if the web server rewrite rules are changed. This can also be set to a static path.
        'baseUri'        => '/',
    ]
]);


35.1k
Accepted
answer

If you look at your folders, the way your app is shown you will have something like this:

/folder/
    /controllers/
        UserEndController.php
    /models/
        Settings.php
....

When you try to load your model (look first snippet of code), you use this:

use \settings\Settings;

That points the loader to try and find the file with the namespace \settings\Settings which I don't think exists (also case sensitivity matters).

Lets make this a bit simpler so that you follow this along easier. Assume that your application name is MyApp. Change your loader to do this

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerNamespaces(
    [
        'MyApp\Controllers' => $config->application->controllersDir,
        'MyApp\Models'      => $config->application->modelsDir,
    ]
)->register();

Now change your model Settings.php to have this line at the top

namespace MyApp\Mmodels;

class Settings extends Phalcon\Mvc\Model
{
...
}

Controller:

namespace MyApp\Controllers;

class UserEndController extends Phalcon\Mvc\Controller
{
...
}

This way you will also speed things up, since the registerNamespaces tells what prefix points to what folder, vs the registerDirs tells the loader to try and find a file/class in the folders registered.



282

My problem is solved but in addition to those who might encounter the problem of loading the routers you should set the default namespace for routers in your configs like below:

$router->setDefaultNamespace('iCriticize\Controllers');