1

domain.com/application/controller/action/parameterN/valueN当用户输入或在网站的直接链接中输入时,我正在使用这样的 URL 。

但我知道,当我实现AJAXjQuery<form>我可能不得不query string在将表单数据发送到服务器时使用。

但是我实现的方式htaccessfront controller我无法检测到使用查询字符串。我能做些什么?实现一种读取查询字符串的方法或在 JavaScript 中创建一个函数来将我的查询字符串变成漂亮的 url?我该怎么做?

我的 .htaccess 文件:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?key=$1 [L]

我的 php 文件:

<?php
    class System{
        private $_url;
        private $_explode;
        public $_controller;
        public $_action;
        public $_params;

        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(){
            $this->_controller = $this->_explode[0];
        }

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

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

            if ( end( $this->_explode ) == null )
                array_pop( $this->_explode );

            $i = 0;
            if( !empty ($this->_explode) ){
                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();
        }

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

        public function run(){
            $controller_path = CONTROLLERS . $this->_controller . 'Controller.php';
            if( !file_exists( $controller_path ) )
                die('Houve um erro. O controller nao existe.');

            require_once( $controller_path );
            $app = new $this->_controller();

                if( !method_exists($app, $this->_action) )
                    die('Esta action nao existe.');

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

1 回答 1

1

您需要在这里使用 QSA 标志:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?key=$1 [L,QSA]

QSA(查询字符串追加)标志保留现有查询字符串,同时通过重写规则添加新查询参数。

参考:Apache mod_rewrite 简介

于 2013-11-07T15:40:59.663 回答