dpxpz37157 2017-05-12 00:35
浏览 87

PHP MVC自定义框架

I have a doubt, that maybe its simple solution, I'm working on an application, using PHP as backend and ExtJS as frontend.

Following the MVC architecture.

Well .. I use apache as web server, all my development in a pc with debian 8, I have the mod_rewrite apache module activated and here is my .htaccess file

RewriteEngine On

RewriteCond% {REQUEST_FILENAME}! -d
RewriteCond% {REQUEST_FILENAME}! -f
RewriteCond% {REQUEST_FILENAME}! -l

RewriteRule ^ (. *) $ Index.php [QSA, L]

# Prevent file browsing
Options -Indexes

I make everything happen first by index.php that would be my front controller ..

Index.php content

define('ENVIRONMENT', isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'development');

/**
 * --------------------------------------------------------------------------------
 * ERROR REPORTING
 * --------------------------------------------------------------------------------
 */
switch (ENVIRONMENT) {
    case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
        break;
    case 'production':
        ini_set('display_errors', 0);
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        break;
    default:
        die(header('HTTP/1.1 503 Service Unavailable.', true, 503));
}

/**
 * --------------------------------------------------------------------------------
 * AUTOLOAD REGISTER
 * --------------------------------------------------------------------------------
 */
if (is_readable('includes/autoload.php')) {
    require_once 'includes/autoload.php';

    new Autoload();
}

/**
 * --------------------------------------------------------------------------------
 * APPLICATION DISPATCHER
 * --------------------------------------------------------------------------------
 */
if (is_readable('system/App.php')) {
    require_once 'system/App.php';

    new App();
}

Autoload.php content

defined('ENVIRONMENT') or exit('Sorry!. No direct script access allowed.');

    class Autoload
    {
        private $_extensions = array(
            0 => '.inc',
            1 => '.php',
            2 => '.lib.php',
            3 => '.class.php',
        );

        public function __construct()
        {
            define('DS', DIRECTORY_SEPARATOR);
            define('PS', PATH_SEPARATOR);

            define('FCFILE', basename($_SERVER['PHP_SELF']));
            define('BASE_PATH', realpath(dirname(__DIR__)) . DS);

            define('SYSTEM_PATH', 'system');
            define('INCLUDES_PATH', 'includes');
            define('CONTROLLERS_PATH', 'controllers');
            define('VIEWS_PATH', 'views');

            self::includePaths();

            spl_autoload_extensions(implode(',', $this->_extensions));
            spl_autoload_register(array(__CLASS__, 'includeFile'));

            self::sessionStarter();
        }

        public function __destruct()
        {

        }

        private static function includePaths()
        {
            set_include_path(implode(PS, array(
                realpath(SYSTEM_PATH),
                realpath(INCLUDES_PATH),
                realpath(CONTROLLERS_PATH),
                realpath(VIEWS_PATH),
                get_include_path(),
            )));
        }

        private static function includeFile($file)
        {
            if (!empty($file)) {
                spl_autoload($file);
            }
        }

        private static function sessionStarter()
        {
            if (!isset($_SESSION)) {
                session_name('AURORA_SESSION');
                session_start();
            }
        }
    }

Well..after follow .. and create my router .. others and others..i want .. when i type in the address bar ...

Http: // localhost / clients / read

If I do it directly .. give me an error .. like CodeIgniter does for example ..

Defined ('ENVIRONMENT') or exit ('Sorry! No direct script access allowed.');

Osea .. what I'm looking for .. is if the request is not made from a page inside the views folder, then it returns an error .. to avoid .. that they execute scripts directly ..

Thank you very much in advance .. and in anticipation of your help ..

  • 写回答

1条回答 默认 最新

  • duanjing7651 2017-05-12 16:18
    关注

    You are doing it wrong.

    The reason why CodeIgniter has that stupid line is simple - they keep all code in the webserver's document root. And that magical line helps then to check, if the file was included was not executed directly. That's a terrible idea.

    Instead, lets assume you have the following directory structure:

    /project
      /docs
      /src
        /application
        /config
        /public
      /tests
    

    In this case, the DOCUMENT_ROOT for your website should be /project/src/public and that directory should contain only CSS/JS/images and one PHP file: something like index.php, which would contain:

    <?php 
    require __DIR__ . '/../application/bootstrap.php';
    

    This way, if mod_php fails, your visitors wont be able to read all your source code (yes, it can happen).

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况