Implement fat-free namespaces

I'm trying to use namespaces in the fatfree framework, but somehow its inability to find the next class is my setup

routes.ini

[routes] GET /=Src\Controllers\Index->index 

index.php

 namespace Src\Controllers; class Index { function index($f3) { $f3->set('name','world'); echo View::instance()->render('template.htm'); } } 

Global index.php

 // Retrieve instance of the framework $f3=require('lib/base.php'); // Initialize CMS $f3->config('config/config.ini'); // Define routes $f3->config('config/routes.ini'); // Execute application $f3->run(); 

UPDATE:

Error:

Not found

HTTP 404 (GET /)

β€’ index.php: 13 Base-> run ()

UPDATE 2:

config.ini

 [globals] ; Where the framework autoloader will look for app files AUTOLOAD=src/controllers/ ; Remove next line (if you ever plan to put this app in production) DEBUG=3 ; Where errors are logged LOGS=tmp/ ; Our custom error handler, so we also get a pretty page for our users ;ONERROR="" ; Where the framework will look for templates and related HTML-support files UI=views/ ; Where uploads will be saved UPLOADS=assets/ 

I'm not sure what will go wrong.

Thanks in advance.

+5
source share
3 answers

Fat-Free Framework autoloader is very simple. He expects you to define one or more startup folders, each of which will map to the root namespace.

So, let's say you define $f3->set('AUTOLOAD','app/;inc/') , and your file structure is:

 - app - inc - lib |- base.php - index.php 

Then a class named MyClass belonging to the Foo\Bar namespace (full path: Foo\Bar\MyClass ) must be stored either in app/foo/bar/myclass.php or inc/foo/bar/myclass.php (remember : we specified two startup folders).

NB : do not forget to specify namespace Foo\Bar at the beginning of myclass.php (the autoloader will not do this for you).

-

So, to answer your specific problem, having the following file structure:

 - lib |- base.php - src |- controllers |- index.php - index.php 

Three configurations are possible:

Configuration 1

$f3->set('AUTOLOAD','src/controllers/')

Then all files under src/controllers/ will be automatically downloaded, but remember: src/controllers/ mapped to the root namespace , therefore, means that the Index class must belong to the root namespace (full path: \Index ).

Configuration 2

$f3->set('AUTOLOAD','src/')

Then all files under src/ will be automatically loaded, which means that the Index class must belong to the Controllers namespace (full path: \Controllers\Index ).

Configuration 3

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

Then all files under ./ will be automatically loaded, which means that the Index class must belong to the Src\Controllers namespace (full path: \Src\Controllers\Index ).

+8
source

Fat-Free is always the root namespace "\". (the following may be wrong). Since F3 loads your classes through the autoloader, you always need to add the root namespace to your own namespaces. In this case, you must change it to

 namespace \Src\Controllers; 

And, of course, you also have to change it on your routes.

 GET /=\Src\Controllers\Index->index 

To help you find these problems in the future, you can increase the DEBUG value with

 $f3->set('DEBUG', 2); // 0-3; 0=off, 3=way too much information 
+1
source

Try this configuration - your class:

 namespace Creo\Controllers; 

Frame routes

 GET|POST / = \Creo\Controllers\IndexController->indexAction 

folder location

 _your_app_dir/app/Creo/Controllers 

Your boot file (in this case in _your_app_dir / app /)

 spl_autoload_register(function ($className) { $filename = __DIR__ . '/' . str_replace('\\', '/', $className) . ".php"; if (file_exists($filename)) { include($filename); if (class_exists($className)) { return true; } } return false; }); 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1208202/


All Articles