简体   繁体   中英

Issue loading config file array within MVC/OOP structured PHP

I am creating a basic MVC structured CMS in PHP as a means to learn how MVC works (thus the reason I am not using a true prebuilt engine). I have a basic version working and am currently trying to shift information over to a config file like so:

config.php

 <?php
    return array(
        //  MySQL database details
        'database' => array(
            'host' => 'localhost',
            'port' => '',
            'username' => '',
            'password' => '',
            'name' => '',
            'collation' => 'utf8_bin'
        ),

        // Application settings
        'application' => array(
            // url paths
            'default_controller' => 'index',
            'default_action' => 'index',
            'timezone' => 'UTC',
        ),

        // Session details
        'session' => array(
            'name' => '',
            'expire' => 3600,
            'path' => '/',
            'domain' => ''
        ),

        // Error handling
        'error' => array(
            'ignore' => array(E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED),
            'detail' => false,
            'log' => false
        )
    );

I am including it in the index.php file like this:

index.php

define ('__SITE_PATH', $site_path);
$config = include __SITE_PATH . '/config.php';

However, when I try to load it later in a template file for testing, or any other file for that matter nothing is returned. Is this an issue with classes? If anyone could shed some light on the matter I'd be very appreciative.

Here is the full index.php for more reference:

<?php

    /*** error reporting on ***/
    error_reporting(E_ALL);

    /*** define the site path ***/
    $site_path = realpath(dirname(__FILE__));
    define ('__SITE_PATH', $site_path);

    $config = include __SITE_PATH . '/config.php';

    /*** include the controller class ***/
    include __SITE_PATH . '/application/' . 'controller_base.class.php';

    /*** include the registry class ***/
    include __SITE_PATH . '/application/' . 'registry.class.php';

    /*** include the router class ***/
    include __SITE_PATH . '/application/' . 'router.class.php';

    /*** include the template class ***/
    include __SITE_PATH . '/application/' . 'template.class.php';

    /*** auto load model classes ***/
    function __autoload($class_name) {
        $filename = strtolower($class_name) . '.class.php';
        $file = __SITE_PATH . '/model/' . $filename;
        if (file_exists($file) == false) {
            return false;
        }
        include ($file);
    }

    /*** a new registry object ***/
    $registry = new registry;

    /*** load the router ***/
    $registry->router = new router($registry);

    /*** set the controller path ***/
    $registry->router->setPath (__SITE_PATH . '/controller');

    /*** load up the template ***/
    $registry->template = new template($registry);

    /*** load the controller ***/
    $registry->router->loader();

I think you are doing this all wrong.

Instead of creating complicated configuration I find it much easier to initialize all the structures at bootstrap stage, and then inject the dependencies where required.

For example, instead to have connection configuration for database, you create a closure, which then can be injected in a factory of data mappers .

$dbhProvider = function()
{
    $instance = new \PDO('mysql:host=localhost;dbname=foobar;charset=UTF-8', 
                         'user', 
                         'password');
    $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    return $instance;
};

And similar principles can be applied for dealing with session management and routing mechanism.

And what the hell is this ?

'ignore' => array(E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED),

Why would you ignore warnings related to your code quality? That is just stupid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM