Following the structure you used, simply create an event in a . php file that will be included within the index.php (which should go inside of public_html
), note that the public_html
will be your root (pro server), however this is not a must, I’m just following your folder structure.
For this you will have to use the spl_autoload_register
, the link http://www.php-fig.org/psr/psr-4/examples/ - I made an example, with some modifications:
<?php
function myAutoLoader($class)
{
// Diretório aonde ficam as bibliotecas
$base_dir = __DIR__ . '/src/App/'; //A pasta que conterá os arquivos que serão autocarregados
//Transforma namespace no caminho do arquivo
$file = $base_dir . str_replace('\\', '/', $class) . '.php';
// Verifica se o arquivo existe, se existir então inclui ele
if (is_file($file)) {
include_once $file;
}
}
spl_autoload_register('myAutoLoader');
You can put it in (or use include
) inside your index.php
, or create a file within Core
and use include
calling him.
For more details, see this answer I made: What is spl_autoloader_register in PHP?
Notes on the code
Note: Other than http://www.php-fig.org I used include_once
instead of require
, because if the file has already been manually included or it is not a class, it will present more errors than it should and the only error that the PSR-4 standard should present is that of undefined class.
Note 2: I used is_file
instead of file_existes
, because although it is almost impossible yet a developer who use your project may end up creating a folder called ./src/App/Controller/foo.php/index.html
, of course this is a user error, but as I said in the other note, PSR-4 shall only contain errors of undefined class (which is the standard) however if the require
trying to call a folder will present an error as it file_exists
returns true
to directories:
Warning: require(src/App/Controller/foo.php): failed to open stream: No such file or directory in Z: www project server.php on line 22
Fatal error: require(): Failed Opening required 'src/App/Controller/foo.php' (include_path='.') in Z: www project server.php on line 22
The reason for this is because it is described here http://www.php-fig.org/psr/psr-4/#2-Specification:
Autoloader implementations MUST NOT release exceptions, must not generate errors of any level and must not return any value.
you have the example on php-fig.org
– Daniel Omine
From what I saw in the PSR-4 implementation example at this link, for every new namespace that I create I should add it along with the file path as in this example:
$loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
. Wanted, however, to make it automatic.– robertaodj
the example is merely didactic... implementations as the automation you want to do, is on account of the
– Daniel Omine
But if you come out of the psr-4 proposal, you’re no longer psr-4, you understand? The pattern serves precisely to avoid those monstrous gambiarras in autoloaders... I myself have created many of these monsters... today I try to follow the psr-4.. It may seem illogical to have several autoloaders files, but it’s better.. In the end you won’t even have much... usually I use only 2, being 1 for core functions and another for the application where contain business models
– Daniel Omine
Yes. As for the use of 2 or more autoloads I see no problem. But having to register the namespaces I found it a little confusing. Apparently there is no restriction to this registration. That would be a step backwards in my opinion.
– robertaodj
i also do not use the "record" technique present in the site example.. alias was the first thing I removed rsrsr
– Daniel Omine