如何自动创建一个php页面&该页面没有.php扩展名

时间:2014-01-11 13:55:13

标签: php mod-rewrite url-rewriting

我想创建新页面只需单击文本,我希望该文本是新页面的名称。但它不包括 .php 扩展名

假设我有一个页面 blog.php blog.php 中,我们有一个图片链接到 single-blog.php ,并带有传递博客名称

blog-single.php?blog=<?php echo $data['title']

这会在网址www.abc.com/blog-single.php?blog=welcome

中创建一个链接

但我希望这样:www.abc.com/blog-single/welcome

3 个答案:

答案 0 :(得分:0)

您需要在Web服务器上使用URL重写。特别是,如果您使用Apache,请启用mod_rewrite并查看如何使用它:

How to enable mod_rewrite for Apache 2.2

答案 1 :(得分:0)

使用PHP创建文件很简单:file_put_contents('filename', $contents);

重写URL有点复杂。一种方法是添加路由器类来处理您的URL请求,并使用.htaccess重写它们,如下所示:

<强> router.class.php:

<?
    class HttpRequest {
        const CONTROLLER_CLASSNAME = 'Index';

        protected $controllerkey = 0;
        protected $baseUrl;
        protected $controllerClassName;
        protected $parameters;

        public function __construct() {
            $this->controllerClassName = self::CONTROLLER_CLASSNAME;
        }

        public function setBaseUrl($url) {
            $this->baseUrl = $url;

            return $this;
        }

        public function setParameters($params) {
            $this->parameters = $params;

            return $this;
        }

        public function getParameters() {
            if($this->parameters == null) {
                $this->parameters = array();
            }

            return $this->parameters;
        }

        public function getControllerClassName() {
            return $this->controllerClassName;
        }

        public function getParam($name, $default = null) {
            if(isset($this->parameters[$name])) {
                return $this->parameters[$name];
            }

            return $default;
        }

        public function getRequestUri() {
            if(!isset($_SERVER['REQUEST_URI'])) {
                return '';
            }

            $uri = $_SERVER['REQUEST_URI'];
            $uri = trim(str_replace($this->baseUrl, '', $uri), '/');

            return $uri;
        }

        public function createRequest() {
            $uri = $this->getRequestUri();
            $uriParts = explode('/', $uri);

            if(!isset($uriParts[$this->controllerkey])) {
                return $this;
            }

            $this->controllerClassName = $this->formatControllerName($uriParts[$this->controllerkey]);

            unset($uriParts[$this->controllerkey]);

            if(empty($uriParts)) {
                return $this;
            }

            $i = 0;
            $keyName = '';

            foreach($uriParts as $key => $value) {
                if ($i == 0) {
                    $this->parameters[$value] = '';
                    $keyName = $value;
                    $i = 1;
                } else {
                    $this->parameters[$keyName] = $value;
                    $i = 0;
                }
            }

            if($_POST) {
                foreach ($_POST as $postKey => $postData) {
                    $this->parameters[$postKey] = $postData;
                }
            }

            return $this;
        }

        protected function formatControllerName($unformatted) {
            if (strpos($unformatted, '-') !== false) {
                $formattedName = array_map('ucwords', explode('-', $unformatted));
                $formattedName = join('', $formattedName);
            } else {
                $formattedName = ucwords($unformatted);
            }

            if (is_numeric(substr($formattedName, 0, 1))) {
                $part = $part == $this->controllerkey ? 'controller' : 'action';
                throw new Exception('Incorrect ' . $part . ' name "' . $formattedName . '".');
            }
            return ltrim($formattedName, '_');
        }
    }
?>

<强> htaccess的:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

要使用它:

<?
    require_once('router.class.php');

    $request = new HttpRequest();
    $request->setBaseUrl('http://url.com/');
    $request->createRequest();
    $value = $request->getParameters();

    echo $value['key'];
?>

答案 2 :(得分:0)

您可以将URL重写用作

RewriteRule ^blog-single/(.+)$ /blog-single.php?blog=$1 [QSA,NC,L]

请参阅tutorial here