2017-08-23 10 views
2

iは自家製MVCを構築している、以下のコードを見てみましょう。 PHPのwww \アプリケーション\コントローラ\"//" 記号:</p> <p>のwww インデックス: "site.com.br//"

<?php 

define('CONTROLLERS', 'app/controllers/'); 
define('VIEWS', 'app/views/'); 
define('MODELS', 'app/models/'); 
define('HELPERS', 'system/helpers/'); 

require_once 'system/system.php'; 
require_once 'system/controller.php'; 
require_once 'system/model.php'; 

function __autoload($file) { 

if (strpos($file, 'Model') !== false) { 

    $filenm = explode('Model', $file); 
    $stringfile = strtolower($filenm[0]); 
    $file = $stringfile . 'Model'; 

    if (file_exists(MODELS . $file . '.php')) { 
     require_once MODELS . $file . '.php'; 
    } else { 
     $msg = 'Model não encontrado'; 
     die($msg); 
    } 
} 
if (strpos($file, 'Helper') !== false) { 

    $filenm = explode('Helper', $file); 
    $stringfile = strtolower($filenm[0]); 
    $file = $stringfile . 'Helper'; 

    if (file_exists(HELPERS . $file . '.php')) { 
     require_once HELPERS . $file . '.php'; 
    } else { 
     $msg = 'Helper não encontrado'; 
     die($msg); 
    } 
} 
} 

$start = new System; 
$start->run(); 

\ indexController.php

<?php 

class IndexController extends Controller { 

public function index_action() { 

    $this->view('index'); 


} 

} 

のwww \システム\のsystem.php

<?php 

class System { 

private $_url; 
private $_explode; 
public $underline_controller = FALSE; 
public $_controller; 
public $_action; 
public $_params; 
public $_params_no_action; 

public function __construct() { 
    $this->setUrl(); 
    $this->setExplode(); 
    $this->setController(); 
    $this->setAction(); 
    $this->setParams(); 
} 

private function setUrl() { 

    $_GET['url'] = (isset($_GET['url']) ? $_GET['url'] : 'index/index_action'); 
    $this->_url = $_GET['url']; 
} 

private function setExplode() { 

    $this->_explode = explode('/', $this->_url); 
} 

private function setController() { 
    //o controller sempre será o primeiro nome do array acima. 
    $this->_controller = $this->_explode[0]; 
    if (strpos($this->_controller, '_') !== false) 
     $this->underline_controller = TRUE; 

    $this->_controller = str_replace("-", "_", $this->_controller); 
} 

private function setAction() { 

    $ac = (!isset($this->_explode[1]) || $this->_explode[1] == null || $this->_explode[1] === 'index' ? 'index_action' : $this->_explode[1]); 
    $this->_action = $ac; 

    $this->_action = str_replace("-", "_", $this->_action); 

} 

private function setParams() { 
    unset($this->_explode[0], $this->_explode[1]); 
    if (end($this->_explode) == null) 
     array_pop($this->_explode); 


    $i = 0; 
    if (count($this->_explode) > 1) { 
     foreach ($this->_explode as $val) { 
      if ($i % 2 == 0) { 
       $ind[] = $val; 
      } else { 
       $value[] = $val; 
      } 
      $i++; 
     } 
    } else { 
     $ind = array(); 
     $value = array(); 
    } 
    if (count($ind) == count($value) && !empty($ind) && !empty($value)) { 
     $this->_params = array_combine($ind, $value); 
    } else { 
     $this->_params = array(); 
    } 


    if (count($this->_explode) == 1) { 
     if (!empty($this->_explode[2])) { 
      $this->_params_no_action = array(0 => $this->_action); 
      $this->_action = $this->_explode[2]; 
     } 
    } 
    if (count($this->_explode) == 0) { 
     $this->_params_no_action = array(0 => $this->_action); 
    } 
} 

public function getParam($name = null) { 
    if ($name != null) 
     return $this->_params[$name]; 
    else 
     return $this->_params; 
} 

public function getParamNoAction($name = null) { 
    if ($name != null) 
     return $this->_params_no_action[$name]; 
    else 
     return $this->_params_no_action; 
} 

public function run() { 

    $controller_path = CONTROLLERS . strtolower($this->_controller) . 'Controller.php'; 
    if (!file_exists($controller_path) || $this->underline_controller) { 
     /* controller not exist */ 
     header('HTTP/1.1 404 Not Found'); 
     header("Refresh:0; url=erro404"); 
    } 

    require_once $controller_path; 
    $this->_controller = ucfirst($this->_controller) . 'Controller'; 
    $app = new $this->_controller(); 

    //se a controller for curso, permite, caso nao, redireciona para o 404... 
    if (!method_exists($app, $this->_action)) 
     $this->_action = 'index_action'; 

    $action = $this->_action; 
    $app->$action(); 
} 

} 

のwww \システム\のcontroller.php

<?php 

class Controller extends System { 

protected function view($nome, $vars = null) { 
    if (is_array($vars) && count($vars) > 0) 
     extract($vars, EXTR_PREFIX_ALL, 'view'); 

    return require_once VIEWS . $nome . '.phtml'; 
} 

} 

のwww \の.htaccessの

<IfModule mod_rewrite.c> 


RewriteEngine on 

RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteCond %{SCRIPT_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?url=$1 

# redirect from http to https 
RewriteCond %{HTTPS} !on 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 
</IfModule> 

<IfModule mod_deflate.c> 

SetOutputFilter DEFLATE 
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary 

<FilesMatch "\.(js|css|html|xml|x?html?|php)$"> 
    AddOutputFilterByType DEFLATE text/plain 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/xml 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE image/svg+xml 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/atom_xml 
    AddOutputFilterByType DEFLATE application/x-javascript 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/json 
    AddOutputFilterByType DEFLATE application/x-httpd-php 
    AddOutputFilterByType DEFLATE application/x-httpd-fastphp 
    AddOutputFilterByType DEFLATE application/x-httpd-eruby 
    AddOutputFilterByType DEFLATE text/html 
</FilesMatch> 

#DeflateCompressionLevel 5 

BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.0[678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 

#DeflateFilterNote Input instream 
#DeflateFilterNote Output outstream 
#DeflateFilterNote Ratio ratio 

#LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate 

</IfModule> 

<IfModule mod_expires.c> 
# Enable expirations 
ExpiresActive On 
# Default directive 
ExpiresDefault "access plus 1 month" 
# My favicon 
ExpiresByType image/x-icon "access plus 1 year" 
# Images 
ExpiresByType image/gif "access plus 1 month" 
ExpiresByType image/png "access plus 1 month" 
ExpiresByType image/jpg "access plus 1 month" 
ExpiresByType image/jpeg "access plus 1 month" 
# CSS 
ExpiresByType text/css "access plus 1 month" 
# Javascript 
ExpiresByType application/javascript "access plus 1 year" 
</IfModule> 

は、私はちょうど、HTTPSをインストールしましたが、問題は後に、私は、ブラウザで "site.com.br" を入力することですURLはブラウザで "https://site.com.br//"に書き換えられます。追加の記号 "//"が付いています。ブラウザにhttps://site.com.brと入力すると、記号 "//"は追加されません。 どこに問題があるのか​​わかりませんので、ここで助けを求めました。誰かが私を助けることができますか?ありがとうございました!

答えて

0

最初にリダイレクトしてからルーティングする必要があります。そうしないと、リダイレクトですでに書き換えられたURIが取得されます。だから、このようにあなたのModRewriteディレクティブを維持してください:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # redirect from http to https 
    RewriteCond %{HTTPS} !on 
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] 

    RewriteCond %{SCRIPT_FILENAME} !-f 

    RewriteCond %{SCRIPT_FILENAME} !-d 

    RewriteRule ^(.*)$ index.php?url=$1 [L] 
</IfModule> 
+0

あなたはとても良いです! –

関連する問題