We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Autoloader - What am i doing wrong

Hello.

I'm trying to setup my autoloader, so I can use my validations inside my models. Here is what i've done so far:

Loader.php:

$loader->registerNamespaces( array( 'Validation' => 'validation/' ) )->register();

var_dump(new Validation\Validate());

My Validate class is inside app/validation/

Why is it, that it can't find the class??

You have to specify the full path to the directory:

$loader->registerNamespaces( array( 'Validation' => '../app/validation/' ) )->register();


4.0k

Loader.php is inside app/ directory? Or inside app/config/? if in app/config/:

$loader->registerNamespaces( array( 'Validation' => __DIR__.'/../validation/' ) )->register();

And app/validation/Validate.php file begin:

<?php
namespace Validation;
....

Thanks for the quick anwers!

My loader was inside config, so i got it to work Paulius - Thank Andres, still the same code tho just diffrent folders :-)

In my model i got a function called validation

Is this the best code?

$validation = new \Validation\Validate();

    $messages = $validation->validate($_POST);
    if (count($messages)) {
        foreach ($messages as $message) {
            echo $message, '<br>';
        }
    }


4.0k

I'm using this.

How would you set your custom rules? That's a lot of code if you want min and max length and so on?



7.9k

did your project follow PSR-4 convention?