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.

I'm newbie. why new model() is not working...

I'm newbie. In Korea, Phalcon is not popular very much. It's hard to get Phalcon information. But I like phalcon. I searched web very much for solve error.

sorry for my english. help, please.

IndexController.php

<?php namespace App\Controllers;

class IndexController extends ControllerBase{

public function indexAction(){

    // output errors to browser
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

   $a = new \App\Models\Cars();

browser

Fatal error: Uncaught Error: Class 'App\Models\Cars' not found in /var/www/html/evasmall/app/app/controllers/IndexController.php:13 Stack trace: #0 [internal function]: App\Controllers\IndexController->indexAction() #1 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(App\Controllers\IndexController), 'indexAction', Array) #2 [internal function]: Phalcon\Dispatcher->dispatch() #3 /var/www/html/evasmall/app/public/index.php(38): Phalcon\Mvc\Application->handle() #4 {main} thrown in /var/www/html/evasmall/app/app/controllers/IndexController.php on line 13

Hi there @joyholdgod. Have you registered the App\Models namespace in your Phalcon Loader?


using \Phalcon\Loader;

$loader = new Loader();

$loader -> registerNamespaces([
    'App\Models'    => '../app/models/'
]);

$loader -> register();
edited Jan '19

Yes, It is. My source is below.

p.s how to code look like your black code?

loader.php

<?php

$loader = new \Phalcon\Loader();

/**

  • We're a registering a set of directories taken from the configuration file */

$loader ->registerNamespaces(

    [

        // 'App\Controllers'       => __DIR__ . '/../controllers/',

        // 'App\Controllers\Admin' => __DIR__ . '/../controllers/admin'

        'App\Controllers'         => $config->application->controllersDir,

        'App\Controllers\Admin'   => $config->application->controllersDir . 'admin/',

        'App\Controllers\Auth'    => $config->application->controllersDir . 'auth/',

            // 'App\Controllers\Auth\Signup'    => $config->application->controllersDir . 'auth/signup',

        'App\Controllers\Partner' => $config->application->controllersDir . 'partner/',

        'App\Controllers\Policy'  => $config->application->controllersDir . 'policy/',

        'App\Controllers\Profile' => $config->application->controllersDir . 'profile/',

        'App\Controllers\Setting' => $config->application->controllersDir . 'setting/',

        'App\Controllers\Trade'   => $config->application->controllersDir . 'trade/',

        'App\Models'              => '../app/models/' // $config->application->modelsDir
    ]
)->register();


279
Accepted
answer

1) First you need to make sure your models are actually being referenced in

    'App\Models'              => '../app/models/' // $config->application->modelsDir

    For example, if this is your directory structure:
    -- app
        | -- config
            | -- config.php
            | -- loader.php
            | -- services.php
        | -- controllers
            | -- IndexController.php
        | -- models
            |-- User.php
        | -- views

Define your BASE_PATH in your config.php or config.ini

    ```php
    defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
    defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');

Then point your model path to:


        'modelsDir'      => APP_PATH . '/models/',

Once thats taken care of, go to your loader.php file, and change the Model autoloading to:
        ```php
        $loader = new \Phalcon\Loader();

        $loader->registerDirs([
                $config->application->controllersDir,
                $config->application->modelsDir
        ]);

        $loader -> registerNamespaces([
                // 'App\Controllers'       => __DIR__ . '/../controllers/',

        // 'App\Controllers\Admin' => __DIR__ . '/../controllers/admin'

        'App\Controllers'         => $config->application->controllersDir,

        'App\Controllers\Admin'   => $config->application->controllersDir . 'admin/',

        'App\Controllers\Auth'    => $config->application->controllersDir . 'auth/',

            // 'App\Controllers\Auth\Signup'    => $config->application->controllersDir . 'auth/signup',

        'App\Controllers\Partner' => $config->application->controllersDir . 'partner/',

        'App\Controllers\Policy'  => $config->application->controllersDir . 'policy/',

        'App\Controllers\Profile' => $config->application->controllersDir . 'profile/',

        'App\Controllers\Setting' => $config->application->controllersDir . 'setting/',

        'App\Controllers\Trade'   => $config->application->controllersDir . 'trade/',

        'App\Models'              => $config->application->modelsDir
        ]);

        $loader -> register();

Once thats complete, then check if your models are created under the namespace:

        ```php
        // app/models/User.php file

        <?php

        namespace App\Models;
        use \Phalcon\Mvc\Model;

        class User extends Model
        {

        }

Good luck....
edited Jan '19

Thank you very much. It workd. Fly Phalcon~~

config/loader.php

$loader->registerDirs([

$config->application->controllersDir,
$config->application->modelsDir

]);

$loader->registerNamespaces( [

    // 'App\Controllers'       => __DIR__ . '/../controllers/',
    // 'App\Controllers\Admin' => __DIR__ . '/../controllers/admin'
    'App\Controllers'         => $config->application->controllersDir,
    'App\Controllers\Admin'   => $config->application->controllersDir . 'admin/',
    'App\Controllers\Auth'    => $config->application->controllersDir . 'auth/',
    // 'App\Controllers\Auth\Signup'    => $config->application->controllersDir . 'auth/signup',
    'App\Controllers\Partner' => $config->application->controllersDir . 'partner/',
    'App\Controllers\Policy'  => $config->application->controllersDir . 'policy/',
    'App\Controllers\Profile' => $config->application->controllersDir . 'profile/',
    'App\Controllers\Setting' => $config->application->controllersDir . 'setting/',
    'App\Controllers\Trade'   => $config->application->controllersDir . 'trade/',

    'App\Models'              => $config->application->modelsDir
]

);

$loader -> register();

models/cars.php

<?php

namespace App\Models;

use Phalcon\Mvc\Model;

class Cars extends Model { public $id; public $name; public $brand_id; public $price; public $year; public $style;

Alright. Glad to be of help. Good luck with your developement...