Answer the question
In order to leave comments, you need to log in
Namespace and class connection?
Started making a web application on Slim.
I can't figure out why the namespace doesn't work.
There is a standard autoload.php in the Vendor folder. In index.php I include it via require. Now I create a ClientsController class in the classes/ClientsController.php file and declare the Controllers namespace. Then in the calling file I write use Controllers as Contrl and call
$myControl = new Contrl\ClientsController;
And it says:
Fatal error: Class 'Controllers\ClientsController' not found in C:\xampp\htdocs\goodfood\public\index.php on line 20
What exactly am I doing wrong?
Answer the question
In order to leave comments, you need to log in
Yuri is right, only in your case you need not "App: src/" folder, but "App: classes/", i.e. the root of your sources. And arrange the files according to the specified PSR-0. ( www.php-fig.org/psr/psr-0 )
You can also use PSR-4 ( www.php-fig.org/psr/psr-4 ) there are slightly different rules.
For example, I do this:
"autoload": {
"psr-4": {
"Appname\\": "src/"
}
}
src/
--Appname/
----Controller/
------IndexController.php
namespace Appname\Controller;
use Appname\Controller\IndexController as Control;
composer dump-autoload -o
in the composer.json file, you need to register autoload.
"autoload": {
"psr-0": {
"App": "src/"
}
},
Another small incomprehensible moment:
Why does everything work for me in the public/index.php file:
use Controller\ClientsController;
$myControl = new ClientsController();
$myControl->hello();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question