Getting the autoloader to work

57 views
Skip to first unread message

Roland Dalmulder

unread,
Jul 19, 2017, 10:20:25 AM7/19/17
to f3-fra...@googlegroups.com
Hello all,

Recently I started to use the F3 framework and finding my way around quite easily, the only thing I can't seem to get to work is the autoloader. I simply get this error:

Class 'Connectors\Exact' not found [D:\wamp\www\exact\source\app\index.php:33]

Here is how I have setup everything.

Folder structure

D:\wamp\www\exact\vendor

This folder contains the Composer libraries including F3

D:\wamp\www\exact\source\app

This folder contains the .htaccess and index.php to get it all started.

D:\wamp\www\exact\source\app\connectors
D:\wamp\www\exact\source\app\connectors\Exact.php
D:\wamp\www\exact\source\app\connectors\Database.php
D:\wamp\www\exact\source\app\models
D:\wamp\www\exact\source\app\models\Customer.php
D:\wamp\www\exact\source\app\models\Order.php

These 2 folders contain the namespaced classes.

In the classes I have code like this:

namespace Connectors;

class Exact {
   
public function connect() {
   
}
}


In my index.php I have this:


require __DIR__ . '/../../vendor/autoload.php';

$f3
= \Base::instance();
$f3
->set('AUTOLOAD', 'app/');

$f3
->route('GET /order',
   
function() {
        $connection
= (new Connectors\Exact)->connect();
   
}
);

The routing works fine but it results in the error

Class 'Connectors\Exact' not found [D:\wamp\www\exact\source\app\index.php:33]


Now I wonder where my mistake is. Can you enlighten me?

Thank you.

ved

unread,
Jul 19, 2017, 10:48:19 AM7/19/17
to Fat-Free Framework
Try using:

Roland Dalmulder

unread,
Jul 19, 2017, 1:06:58 PM7/19/17
to Fat-Free Framework
Thanks for your input. I have tried that as well but gives me the same error:

Class 'Connectors\Exact' not found [D:\wamp\www\exact\source\app\index.php:32]

I guess that is because the first \ is the global domain in PHP.

ved

unread,
Jul 19, 2017, 2:41:30 PM7/19/17
to Fat-Free Framework
Hi, sorry I didn't have much time earlier to thoroughly read your post, was at work :)

It appears as your autoload folder is the same folder where index.php is located. Then when you set your autoload path to "app/" I guess F3 assumes you'll have a subfolder "app" with your namespaced stuff.

Create an "app" folder inside your current "app" folder and place the namespaced stuff in there (the Connectors and Models folders). So you would have:

D:\wamp\www\exact\source\app\index.php
D:\wamp\www\exact\source\app\app\connectors
D:\wamp\www\exact\source\app\app\connectors\Exact.php
D:\wamp\www\exact\source\app\app\connectors\Database.php
D:\wamp\www\exact\source\app\app\models
D:\wamp\www\exact\source\app\app\models\Customer.php
D:\wamp\www\exact\source\app\app\models\Order.php

Alternatively, just place the index.php inside the source folder and adjust your webserver root accordingly.

Hope it helps.

Cheers

Roland Dalmulder

unread,
Jul 20, 2017, 2:31:12 AM7/20/17
to Fat-Free Framework
Hello Ved,

Thank you for following up. You were correct. The index.php is in the same base folder as where the autoloader was pointing. So moving it all to a subfolder named app/ did work. What also works is using this:

$f3->set('AUTOLOAD', '../app/');

It is all about path resolution :)

Going to have a second look at how I structured things but having it working is great. Thanks again.
Reply all
Reply to author
Forward
0 new messages