1
votes

So I have Users model namespaced:

namespace App\Models;

use Phalcon\Mvc\Model;

class Users extends Model
{

And Controller:

use \Phalcon\Mvc\Controller;
use \App\Models\Users;

class LoginController extends Controller
{
  ...
  public function postLoginAction() {
    $user = Users::findFirst([ ...
  }
  ...

Always getting error Fatal error: Uncaught Error: Class 'App\Models\Users' not found in no matter what, tried every variation no idea what's wrong with it. even my IDEA resolving this class correctly.

Everything is working if I remove my namespacing.

UPDATE:

So if you generated project with phalcon dev tools and then started generate other things (model, controllers) you will need to provide namespace for model and it will not work, you will also need to update app/config/loader.php like this:

<?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();

$loader->registerNamespaces(
    [
        'App\Controller' => $config->application->controllersDir,
        'App\Model' => $config->application->modelsDir,
    ]
)->register();

In other words register namespace, I don't know why this not clearly stated in documentation but it seams this framework have many such "black holes" or I don't understand this framework fundamentals (came from codeigniter and Laravel background).

1
Have you added the the App\Model namespace to your autoloader? Don't forget to run composer dump-autoload, too. - Emile Pels
I'm not using composer here this is phalcon default project generated with devtools. - RomkaLTU
Yeah, Phalcon does not come with everything working out of the box. You have to choose the parts and glue them together. Something which can be good or bad depending on your project and you. However, its insane speed is what sets it apart (among PHP frameworks), and from experience, this really gives you peace of mind when doing apps for much more traffic. - YOMorales

1 Answers

1
votes

Try this in your bootstrap file:

// registers namespaces
$loader = new Loader();
$loader->registerNamespaces([
    'App\Controllers' => APP_PATH . '/controllers/',
    'App\Models' => APP_PATH . '/models/'
]);
$loader->register();

Btw, APP_PATH could be a constant to your app's directory, or whatever you want/need.