File directory and PHP MVC startup problem

I am creating an MVC PHP framework, and I have some problems with the autoload of my classes, which I think may be lower than my file structure.

Firstly, here is my file structure:

enter image description here

Im testing developing in the local Xampp server environment, so I have private and public folders. Ultimately, on a real server, private folders will be located in the root of the server, and shared folders will be in the public_html directory.

Is my file structure correct? In a private, frontend folder, I have all my different modules in directories that have an inturn controller, a model, viewing files that contain the necessary files for each module.

I have created this autoloader so far:

    //Require config and router classes
require_once('router.class.php');
require_once('config.php');

spl_autoload_register(null, false);

spl_autoload_extensions('.class.php');

function autoloader_core($class){
    $filename = strtolower($class) . '.class.php';

    if(!file_exists($filename)){
        echo $filename . " not found";
    }
    include $filename;
}

function autoloader_app($class){
    $filename = strtolower($class) . '.class.php';
    $file = 'C:\xampp\htdocs\simplebids\simplebidsprivate\app/' . $filename;

    if(!file_exists($file)){
        echo $file . " not found";
    }
    include $file;
}

spl_autoload_register('autoloader_core');
spl_autoload_register('autoloader_app');

$router = new test;
$router->say_hello();

lib- > core, , , test.class.php lib- > core, , , , , , "autoloader_app".

, , echo say_hello() .

, , , , print.

, ?

, frontend, , , // ? ? , ?

+4
1

. , ( MVC)

:

  • // .
  • /trait/interface lowercased, . . Foo "foo.php"
  • /trait/interface, , , . class\Foo\Bar\Baz "/foo/bar/baz.php"

, , - :

(y/ies), / , php:

set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . "/lib");

:

spl_autoload_register('spl_autoload', false);

, , :

Project Root/
|-apptopnamespace/
| |-controllers/
| | |-dashboard.php
| | |-users.php   
| |-models/
| | |-user.php   
| | |-item.php   
| |-etc/
| |-appname.php
| |-router.php
|-bootstrap.php

, , , ..

, bootstrap.php. (, [__DIR__, __DIR__ . DIRECTORY_SEPARATOR . 'lib'], lib):

Bamboo::configure([__DIR__]);

( ) :

/**
 * Add one or more paths to the include path
 *
 * @param array $paths an array of paths to add to the include path
 */
protected static function addIncludePath(array $paths) {
    set_include_path(
        get_include_path() . PATH_SEPARATOR . implode(
            PATH_SEPARATOR,
            $paths
        )
    );
}

/**
 * Register the SPL Autoloader
 */
protected static function setupAutoload() {
    // Don't clobber any __autoload already defined
    if (function_exists('__autoload')) {
        spl_autoload_register('__autoload', false);
    }

    spl_autoload_register('spl_autoload', false);
}
+1

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


All Articles